Skip to content

Commit 5b037d0

Browse files
authored
Register persistent service for declarative model (#35359)
1 parent 943527d commit 5b037d0

File tree

4 files changed

+68
-4
lines changed

4 files changed

+68
-4
lines changed

aspnetcore/blazor/components/prerender.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ In the following example that serializes state for multiple components of the sa
199199
}
200200
```
201201

202-
In the following example that serializes state for a service:
202+
In the following example that serializes state for a dependency injection service:
203203

204204
* Properties annotated with the `[SupplyParameterFromPersistentComponentState]` attribute are serialized during prerendering and deserialized when the app becomes interactive.
205205
* The `AddPersistentService` method is used to register the service for persistence. The render mode is required because the render mode can't be inferred from the service type. Use any of the following values:
@@ -211,6 +211,8 @@ In the following example that serializes state for a service:
211211
> [!NOTE]
212212
> Only persisting scoped services is supported.
213213
214+
<!-- UPDATE 10.0 - Flesh out with a fully-working example. -->
215+
214216
`CounterService.cs`:
215217

216218
```csharp

aspnetcore/blazor/fundamentals/static-files.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,15 @@ In the project file (`.csproj`), the `<WriteImportMapToHtml>` property is set to
240240

241241
When resolving imports for JavaScript interop, the import map is used by the browser resolve fingerprinted files.
242242

243+
Any script in `index.html` with the fingerprint marker is fingerprinted by the framework. For example, a script file named `scripts.js` in the app's `wwwroot/js` folder is fingerprinted by adding `#[.{fingerprint}]` before the file extension (`.js`):
244+
245+
```html
246+
<script src="js/scripts#[.{fingerprint}].js"></script>
247+
```
248+
243249
## Fingerprint client-side static assets in Blazor Web Apps
244250

245-
For client-side rendering (CSR) in Blazor Web Apps (Interactive Auto or Interactive WebAssembly render modes), static asset server-side [fingerprinting](https://wikipedia.org/wiki/Fingerprint_(computing)) is enabled by adopting [Map Static Assets routing endpoint conventions (`MapStaticAssets`)](xref:fundamentals/map-static-files), [`ImportMap` component](xref:blazor/fundamentals/static-files#importmap-component), and the <xref:Microsoft.AspNetCore.Components.ComponentBase.Assets?displayProperty=nameWithType> property (`@Assets["..."]`).
251+
For client-side rendering (CSR) in Blazor Web Apps (Interactive Auto or Interactive WebAssembly render modes), static asset server-side [fingerprinting](https://wikipedia.org/wiki/Fingerprint_(computing)) is enabled by adopting [Map Static Assets routing endpoint conventions (`MapStaticAssets`)](xref:fundamentals/map-static-files), [`ImportMap` component](xref:blazor/fundamentals/static-files#importmap-component), and the <xref:Microsoft.AspNetCore.Components.ComponentBase.Assets?displayProperty=nameWithType> property (`@Assets["..."]`). For more information, see <xref:fundamentals/map-static-files>.
246252

247253
To fingerprint additional JavaScript modules for CSR, use the `<StaticWebAssetFingerprintPattern>` item in the app's project file (`.csproj`). In the following example, a fingerprint is added for all developer-supplied `.mjs` files in the app:
248254

aspnetcore/blazor/state-management.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,37 @@ An app can only persist *app state*. UIs can't be persisted, such as component i
5757

5858
Common locations exist for persisting state:
5959

60+
:::moniker range=">= aspnetcore-10.0"
61+
62+
* [Declarative model for persistent state](#declarative-model-for-persisting-state-server)
63+
* [Server-side storage](#server-side-storage-server)
64+
* [URL](#url-server)
65+
* [Browser storage](#browser-storage-server)
66+
* [In-memory state container service](#in-memory-state-container-service)
67+
* [Cascading values and parameters](#cascading-values-and-parameters)
68+
69+
:::moniker-end
70+
71+
:::moniker range="< aspnetcore-10.0"
72+
6073
* [Server-side storage](#server-side-storage-server)
6174
* [URL](#url-server)
6275
* [Browser storage](#browser-storage-server)
6376
* [In-memory state container service](#in-memory-state-container-service)
6477
* [Cascading values and parameters](#cascading-values-and-parameters)
6578

79+
:::moniker-end
80+
81+
:::moniker range=">= aspnetcore-10.0"
82+
83+
<h2 id="declarative-model-for-persisting-state-server">Declarative model for persisting state</h2>
84+
85+
<!-- UPDATE 10.0 - API cross-link -->
86+
87+
Establish declarative state in a dependency injection service for use around the app by calling `RegisterPersistentService` on the Razor components builder (<xref:Microsoft.Extensions.DependencyInjection.RazorComponentsServiceCollectionExtensions.AddRazorComponents%2A>) with a custom service type and render mode. For more information, see <xref:blazor/components/prerender#persist-prerendered-state>.
88+
89+
:::moniker-end
90+
6691
<h2 id="server-side-storage-server">Server-side storage</h2>
6792

6893
For permanent data persistence that spans multiple users and devices, the app can use server-side storage. Options include:
@@ -618,12 +643,37 @@ An app can only persist *app state*. UIs can't be persisted, such as component i
618643

619644
Common locations exist for persisting state:
620645

646+
:::moniker range=">= aspnetcore-10.0"
647+
648+
* [Declarative model for persistent state](#declarative-model-for-persisting-state-wasm)
649+
* [Server-side storage](#server-side-storage-wasm)
650+
* [URL](#url-wasm)
651+
* [Browser storage](#browser-storage-wasm)
652+
* [In-memory state container service](#in-memory-state-container-service)
653+
* [Cascading values and parameters](#cascading-values-and-parameters)
654+
655+
:::moniker-end
656+
657+
:::moniker range="< aspnetcore-10.0"
658+
621659
* [Server-side storage](#server-side-storage-wasm)
622660
* [URL](#url-wasm)
623661
* [Browser storage](#browser-storage-wasm)
624662
* [In-memory state container service](#in-memory-state-container-service)
625663
* [Cascading values and parameters](#cascading-values-and-parameters)
626664

665+
:::moniker-end
666+
667+
:::moniker range=">= aspnetcore-10.0"
668+
669+
<h2 id="declarative-model-for-persisting-state-wasm">Declarative model for persisting state</h2>
670+
671+
<!-- UPDATE 10.0 - API cross-link -->
672+
673+
Establish declarative state in a dependency injection service for use around the app by calling `RegisterPersistentService` on the Razor components builder (<xref:Microsoft.Extensions.DependencyInjection.RazorComponentsServiceCollectionExtensions.AddRazorComponents%2A>) with a custom service type and render mode. For more information, see <xref:blazor/components/prerender#persist-prerendered-state>.
674+
675+
:::moniker-end
676+
627677
<h2 id="server-side-storage-wasm">Server-side storage</h2>
628678

629679
For permanent data persistence that spans multiple users and devices, the app can use independent server-side storage accessed via a web API. Options include:

aspnetcore/release-notes/aspnetcore-10/includes/blazor.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ Last year, the release of .NET 9 introduced [server-side fingerprinting](https:/
130130

131131
In standalone Blazor WebAssembly apps during build/publish, the framework overrides placeholders in `index.html` with values computed during build to fingerprint static assets. A fingerprint is placed into the `blazor.webassembly.js` script file name.
132132

133-
The following changes must be made in the `wwwwoot/index.html` file to adopt the fingerprinting feature. The standalone Blazor WebAssembly project template will be updated to include these changes in an upcoming preview release:
133+
The following markup must be present in the `wwwwoot/index.html` file to adopt the fingerprinting feature:
134134

135135
```diff
136136
<head>
@@ -161,6 +161,12 @@ In the project file (`.csproj`), add the `<WriteImportMapToHtml>` property set t
161161
</Project>
162162
```
163163

164+
Any script in `index.html` with the fingerprint marker is fingerprinted by the framework. For example, a script file named `scripts.js` in the app's `wwwroot/js` folder is fingerprinted by adding `#[.{fingerprint}]` before the file extension (`.js`):
165+
166+
```html
167+
<script src="js/scripts#[.{fingerprint}].js"></script>
168+
```
169+
164170
To fingerprint additional JS modules in standalone Blazor WebAssembly apps, use the `<StaticWebAssetFingerprintPattern>` property in the app's project file (`.csproj`).
165171

166172
In the following example, a fingerprint is added for all developer-supplied `.mjs` files in the app:
@@ -281,7 +287,7 @@ else
281287
}
282288
```
283289

284-
For more information, see <xref:blazor/components/prerender?view=aspnetcore-10.0#persist-prerendered-state>. Additional API implementation notes, which are subject to change at any time, are available in [[Blazor] Support for declaratively persisting component and services state (`dotnet/aspnetcore` #60634)](https://github.com/dotnet/aspnetcore/pull/60634).
290+
State can be serialized for multiple components of the same type, and you can establish declarative state in a service for use around the app by calling `RegisterPersistentService` on the Razor components builder (<xref:Microsoft.Extensions.DependencyInjection.RazorComponentsServiceCollectionExtensions.AddRazorComponents%2A>) with a custom service type and render mode. For more information, see <xref:blazor/components/prerender?view=aspnetcore-10.0#persist-prerendered-state>.
285291

286292
<!-- PREVIEW 4
287293

0 commit comments

Comments
 (0)