title | author | description | monikerRange | ms.author | ms.custom | ms.date | uid |
---|---|---|---|---|---|---|---|
Prerender ASP.NET Core Razor components |
guardrex |
Learn about Razor component prerendering in ASP.NET Core Blazor apps. |
>= aspnetcore-8.0 |
riande |
mvc |
11/14/2023 |
blazor/components/prerender |
This article explains Razor component prerendering scenarios for server-rendered components in Blazor Web Apps.
Prerendering is the process of initially rendering page content on the server without enabling event handlers for rendered controls. The server outputs the HTML UI of the page as soon as possible in response to the initial request, which makes the app feel more responsive to users. Prerendering can also improve Search Engine Optimization (SEO) by rendering content for the initial HTTP response that search engines use to calculate page rank.
Without persisting prerendered state, state used during prerendering is lost and must be recreated when the app is fully loaded. If any state is created asynchronously, the UI may flicker as the prerendered UI is replaced when the component is rerendered.
Consider the following PrerenderedCounter1
counter component. The component sets an initial random counter value during prerendering in OnInitialized
lifecycle method. After the SignalR connection to the client is established, the component rerenders, and the initial count value is replaced when OnInitialized
executes a second time.
Components/Pages/PrerenderedCounter1.razor
:
@page "/prerendered-counter-1"
@rendermode InteractiveServer
@inject ILogger<PrerenderedCounter1> Logger
<PageTitle>Prerendered Counter 1</PageTitle>
<h1>Prerendered Counter 1</h1>
<p role="status">Current count: @currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
@code {
private int currentCount;
private Random r = new Random();
protected override void OnInitialized()
{
currentCount = r.Next(100);
Logger.LogInformation("currentCount set to {Count}", currentCount);
}
private void IncrementCount()
{
currentCount++;
}
}
Run the app and inspect logging from the component:
:::no-loc text="info: BlazorSample.Components.Pages.PrerenderedCounter1[0]":::
:::no-loc text=" currentCount set to 41":::
:::no-loc text="info: BlazorSample.Components.Pages.PrerenderedCounter1[0]":::
:::no-loc text=" currentCount set to 92":::
The first logged count occurs during prerendering. The count is set again after prerendering when the component is rerendered. There's also a flicker in the UI when the count updates from 41 to 92.
To retain the initial value of the counter during prerendering, Blazor supports persisting state in a prerendered page using the xref:Microsoft.AspNetCore.Components.PersistentComponentState service (and for components embedded into pages or views of Razor Pages or MVC apps, the Persist Component State Tag Helper).
To preserve prerendered state, decide what state to persist using the xref:Microsoft.AspNetCore.Components.PersistentComponentState service. xref:Microsoft.AspNetCore.Components.PersistentComponentState.RegisterOnPersisting%2A?displayProperty=nameWithType registers a callback to persist the component state before the app is paused. The state is retrieved when the app resumes.
The following example demonstrates the general pattern:
- The
{TYPE}
placeholder represents the type of data to persist. - The
{TOKEN}
placeholder is a state identifier string.
@implements IDisposable
@inject PersistentComponentState ApplicationState
...
@code {
private {TYPE} data;
private PersistingComponentStateSubscription persistingSubscription;
protected override async Task OnInitializedAsync()
{
persistingSubscription =
ApplicationState.RegisterOnPersisting(PersistData);
if (!ApplicationState.TryTakeFromJson<{TYPE}>(
"{TOKEN}", out var restored))
{
data = await ...;
}
else
{
data = restored!;
}
}
private Task PersistData()
{
ApplicationState.PersistAsJson("{TOKEN}", data);
return Task.CompletedTask;
}
void IDisposable.Dispose()
{
persistingSubscription.Dispose();
}
}
The following counter component example persists counter state during prerendering and retrieves the state to initialize the component.
Components/Pages/PrerenderedCounter2.razor
:
@page "/prerendered-counter-2"
@rendermode InteractiveServer
@implements IDisposable
@inject ILogger<PrerenderedCounter2> Logger
@inject PersistentComponentState ApplicationState
<PageTitle>Prerendered Counter 2</PageTitle>
<h1>Prerendered Counter 2</h1>
<p role="status">Current count: @currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
@code {
private int currentCount;
private Random r = new Random();
private PersistingComponentStateSubscription persistingSubscription;
protected override void OnInitialized()
{
persistingSubscription =
ApplicationState.RegisterOnPersisting(PersistCount);
if (!ApplicationState.TryTakeFromJson<int>(
"count", out var restoredCount))
{
currentCount = r.Next(100);
Logger.LogInformation("currentCount set to {Count}", currentCount);
}
else
{
currentCount = restoredCount!;
Logger.LogInformation("currentCount restored to {Count}", currentCount);
}
}
private Task PersistCount()
{
ApplicationState.PersistAsJson("count", currentCount);
return Task.CompletedTask;
}
void IDisposable.Dispose()
{
persistingSubscription.Dispose();
}
private void IncrementCount()
{
currentCount++;
}
}
When the component executes, currentCount
is only set once during prerendering. The value is restored when the component is rerendered:
:::no-loc text="info: BlazorSample.Components.Pages.PrerenderedCounter2[0]":::
:::no-loc text=" currentCount set to 96":::
:::no-loc text="info: BlazorSample.Components.Pages.PrerenderedCounter2[0]":::
:::no-loc text=" currentCount restored to 96":::
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.
For components embedded into a page or view of a Razor Pages or MVC app, you must add the Persist Component State Tag Helper with the <persist-component-state />
HTML tag inside the closing </body>
tag of the app's layout. This is only required for Razor Pages and MVC apps. For more information, see xref:mvc/views/tag-helpers/builtin-th/persist-component-state-tag-helper.
Pages/Shared/_Layout.cshtml
:
<body>
...
<persist-component-state />
</body>
Prerendering guidance is organized in the Blazor documentation by subject matter. The following links cover all of the prerendering guidance throughout the documentation set by subject:
- Fundamentals
- xref:Microsoft.AspNetCore.Components.Routing.Router.OnNavigateAsync is executed twice when prerendering: Handle asynchronous navigation events with
OnNavigateAsync
- Startup: Control headers in C# code
- Handle Errors: Prerendering
- SignalR: Prerendered state size and SignalR message size limit
- xref:Microsoft.AspNetCore.Components.Routing.Router.OnNavigateAsync is executed twice when prerendering: Handle asynchronous navigation events with
- Render modes: Prerendering
- Components
- Control
<head>
content during prerendering - Razor component lifecycle subjects that pertain to prerendering
- Component initialization (
OnInitialized{Async}
) - After component render (
OnAfterRender{Async}
) - Stateful reconnection after prerendering
- Prerendering with JavaScript interop: This section also appears in the two JS interop articles on calling JavaScript from .NET and calling .NET from JavaScript.
- Component initialization (
- QuickGrid component sample app: The QuickGrid for Blazor sample app is hosted on GitHub Pages. The site loads fast thanks to static prerendering using the community-maintained
BlazorWasmPrerendering.Build
GitHub project. - Prerendering when integrating components into Razor Pages and MVC apps
- Control
- Authentication and authorization
- Server-side threat mitigation: Cross-site scripting (XSS)
- Unauthorized content display while prerendering with a custom
AuthenticationStateProvider
- WebAssembly prerendering support
- State management: Handle prerendering: Besides the Handle prerendering section, several of the article's other sections include remarks on prerendering.