You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: aspnetcore/blazor/components/prerender.md
+33-5Lines changed: 33 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -89,14 +89,32 @@ The following counter component example persists counter state during prerenderi
89
89
90
90
protected override void OnInitialized()
91
91
{
92
-
CurrentCount ??= Random.Shared.Next(100);
93
-
Logger.LogInformation("CurrentCount set to {Count}", CurrentCount);
92
+
if (CurrentCount == 0)
93
+
{
94
+
CurrentCount = Random.Shared.Next(100);
95
+
Logger.LogInformation("CurrentCount set to {Count}", CurrentCount);
96
+
}
97
+
else
98
+
{
99
+
Logger.LogInformation("CurrentCount restored to {Count}", CurrentCount);
100
+
}
101
+
94
102
}
95
103
96
104
private void IncrementCount() => CurrentCount++;
97
105
}
98
106
```
99
107
108
+
When the component executes, `CurrentCount` is only set once during prerendering. The value is restored when the component is rerendered. The following is example output.
109
+
110
+
> [!NOTE]
111
+
> If the app adopts [interactive routing](xref:blazor/fundamentals/routing#static-versus-interactive-routing) and the page is reached via an internal [enhanced navigation](xref:blazor/fundamentals/routing#enhanced-navigation-and-form-handling), prerendering doesn't occur. Therefore, you must perform a full page reload for the component to see the following output. For more information, see the [Interactive routing and prerendering](#interactive-routing-and-prerendering) section.
> :::no-loc text=" CurrentCount restored to 96":::
117
+
100
118
In the following example that serializes state for multiple components of the same type:
101
119
102
120
* Properties annotated with the `[SupplyParameterFromPersistentComponentState]` attribute are serialized and deserialized during prerendering.
@@ -283,6 +301,16 @@ The following counter component example persists counter state during prerenderi
283
301
}
284
302
```
285
303
304
+
When the component executes, `currentCount` is only set once during prerendering. The value is restored when the component is rerendered. The following is example output.
305
+
306
+
> [!NOTE]
307
+
> If the app adopts [interactive routing](xref:blazor/fundamentals/routing#static-versus-interactive-routing) and the page is reached via an internal [enhanced navigation](xref:blazor/fundamentals/routing#enhanced-navigation-and-form-handling), prerendering doesn't occur. Therefore, you must perform a full page reload for the component to see the following output. For more information, see the [Interactive routing and prerendering](#interactive-routing-and-prerendering) section.
When the component executes, `currentCount` is only set once during prerendering. The value is restored when the component is rerendered. The following is example output.
346
372
347
373
> [!NOTE]
348
-
> If the app adopts [interactive routing](xref:blazor/fundamentals/routing#static-versus-interactive-routing) and the page is reached via an internal [enhanced navigation](xref:blazor/fundamentals/routing#enhanced-navigation-and-form-handling), prerendering doesn't occur. Therefore, you must perform a full page reload for the `PrerenderedCounter2`component to see the following output. For more information, see the [Interactive routing and prerendering](#interactive-routing-and-prerendering) section.
374
+
> If the app adopts [interactive routing](xref:blazor/fundamentals/routing#static-versus-interactive-routing) and the page is reached via an internal [enhanced navigation](xref:blazor/fundamentals/routing#enhanced-navigation-and-form-handling), prerendering doesn't occur. Therefore, you must perform a full page reload for the component to see the following output. For more information, see the [Interactive routing and prerendering](#interactive-routing-and-prerendering) section.
> :::no-loc text=" currentCount restored to 96":::
354
380
381
+
:::moniker-end
382
+
355
383
By initializing components with the same state used during prerendering, any expensive initialization steps are only executed once. The rendered UI also matches the prerendered UI, so no flicker occurs in the browser.
356
384
357
385
The persisted prerendered state is transferred to the client, where it's used to restore the component state. During client-side rendering (CSR, `InteractiveWebAssembly`), the data is exposed to the browser and must not contain sensitive, private information. During interactive server-side rendering (interactive SSR, `InteractiveServer`), [ASP.NET Core Data Protection](xref:security/data-protection/introduction) ensures that the data is transferred securely. The `InteractiveAuto` render mode combines WebAssembly and Server interactivity, so it's necessary to consider data exposure to the browser, as in the CSR case.
0 commit comments