Support Prerendering

作者 dotnet0608d8924cd3MIT5.5K 個星標收錄於 2026年10月8日更新於 2026年10月8日儲存庫今天更新

Make interactive Blazor components work correctly with prerendering. USE FOR fixing duplicate data loads, UI flicker during prerender-to-interactive handoff, null references during prerender, persisting state across prerender, disabling prerendering, excluding pages from interactive routing, or detecting whether a component is currently prerendering. DO NOT USE for choosing which render mode to use (see create-blazor-project) or general component authoring (see author-component).

AI 產生的概覽

指導修正 Blazor 預先轉譯問題,例如重複載入資料、畫面閃爍,以及互動元件中的空值參考。

功能
此技能說明 Blazor 預先轉譯的運作方式及其對互動元件的影響,包括 OnInitializedAsync 會執行兩次,以及預先轉譯期間不會呼叫 OnAfterRenderAsync。它介紹使用 PersistentState 屬性屬性或 PersistentComponentState 服務,在預先轉譯到互動的交接過程中保留狀態,以及在元件、執行個體或應用程式層級停用預先轉譯的方法。它也涵蓋將頁面排除於互動式路由之外、使用 RendererInfo 在執行階段區分預先轉譯與互動狀態,以及處理預先轉譯期間失敗的用戶端服務。
適用情境
當互動式 Blazor 元件重複載入資料、在預先轉譯到互動的交接過程中閃爍、在預先轉譯期間擲回空值參考,或需要在預先轉譯過程中保留狀態時使用。它也適用於頁面需要 HttpContext、必須停用預先轉譯,或程式碼只能在互動式執行階段附加後執行的情況。
執行需求
需要一個具有互動式轉譯模式(Server、WebAssembly 或 Auto)的 Blazor 專案,並能存取專案的 AGENTS.md 以了解互動模式和範圍。僅為說明性內容;不需要指令碼、套件或認證。

Support Prerendering

How Prerendering Works

Prerendering is on by default for all interactive render modes. The server renders the component as static HTML and ships it to the browser immediately. Then the interactive runtime (Server/WebAssembly) loads and re-renders the component with full interactivity.

This means:

  • OnInitializedAsync runs twice — once during prerender (static), once when the interactive runtime attaches.
  • OnAfterRenderAsync is NOT called during prerender — only after the interactive render.
  • Internal navigation between interactive pages (interactive routing) skips prerendering — prerendering only happens on full page loads.

Step 1 — Read the Project's AGENTS.md

Check the project's AGENTS.md for the Interactivity Mode and Interactivity Scope:

ModePrerendering applies?
None (Static SSR)No — there's no interactive handoff
ServerYes
WebAssemblyYes
AutoYes

If the mode is None, this skill doesn't apply.

Persist State Across Prerender → Interactive

The most common prerendering problem: data loaded in OnInitializedAsync during prerender is thrown away and re-fetched when the interactive runtime attaches. This causes flicker and duplicate API/DB calls.

Recommended: [PersistentState] attribute

Annotate properties to automatically serialize during prerender and restore on interactive activation:

razor
@page "/forecasts"@rendermode InteractiveServer
<h1>Weather</h1>
@if (Forecasts is null){    <p>Loading...</p>}else{    @foreach (var f in Forecasts)    {        <p>@f.Date: @f.TemperatureC°C</p>    }}
@code {    [PersistentState]    public WeatherForecast[]? Forecasts { get; set; }
    protected override async Task OnInitializedAsync()    {        Forecasts ??= await ForecastService.GetForecastsAsync();    }}

The ??= pattern is critical — it means "only fetch if the property wasn't already restored from prerender state."

Multiple instances of the same component

When the same component type appears multiple times, use @key to disambiguate state:

razor
@foreach (var item in items){    <ItemCard @key="item.Id" />}

Advanced: PersistentComponentState service

For complex scenarios (dynamic keys, custom serialization), use the imperative API:

csharp
@inject PersistentComponentState ApplicationState
@code {    private List<Order>? orders;
    protected override async Task OnInitializedAsync()    {        ApplicationState.RegisterOnPersisting(PersistOrders);
        if (!ApplicationState.TryTakeFromJson<List<Order>>("orders", out var restored))        {            orders = await OrderService.GetOrdersAsync();        }        else        {            orders = restored;        }    }
    private Task PersistOrders()    {        ApplicationState.PersistAsJson("orders", orders);        return Task.CompletedTask;    }}

Disable Prerendering

Disable prerendering when a component depends on browser APIs immediately or when the prerender+interactive double render causes problems you can't solve with [PersistentState].

On a component definition

razor
@rendermode @(new InteractiveServerRenderMode(prerender: false))

Replace InteractiveServerRenderMode with InteractiveWebAssemblyRenderMode or InteractiveAutoRenderMode as needed.

On a component instance

razor
<MyChart @rendermode="new InteractiveServerRenderMode(prerender: false)" />

On the entire app

In App.razor:

razor
<HeadOutlet @rendermode="new InteractiveServerRenderMode(prerender: false)" /><Routes @rendermode="new InteractiveServerRenderMode(prerender: false)" />

Note: A parent's prerendering setting overrides children. If <Routes> disables prerendering, individual pages cannot re-enable it.

Exclude Pages from Interactive Routing

In a globally interactive app, some pages may need HttpContext (cookies, request headers, response status codes). These pages must render via static SSR, not inside the interactive runtime.

Use [ExcludeFromInteractiveRouting]:

razor
@page "/privacy"@attribute [ExcludeFromInteractiveRouting]
<h1>Privacy Policy</h1>

This forces a full page reload when navigating to this page, exiting interactive routing. The page renders as static SSR with full HttpContext access.

In App.razor, conditionally apply the render mode:

razor
<!DOCTYPE html><html><head>    <HeadOutlet @rendermode="RenderModeForPage" /></head><body>    <Routes @rendermode="RenderModeForPage" />    <script src="_framework/blazor.web.js"></script></body></html>
@code {    [CascadingParameter]    public HttpContext HttpContext { get; set; } = default!;
    private IComponentRenderMode? RenderModeForPage =>        HttpContext.AcceptsInteractiveRouting() ? InteractiveServer : null;}

Replace InteractiveServer with the app's configured render mode.

Detect Prerender vs Interactive at Runtime

Use RendererInfo to guard code that should only run interactively:

csharp
protected override async Task OnInitializedAsync(){    if (RendererInfo.IsInteractive)    {        // Only runs during the interactive render, not during prerender        await StartSignalRConnection();    }}

RendererInfo properties:

  • IsInteractive — false during prerender, true after interactive runtime attaches
  • Name — "Static" during prerender, "Server" or "WebAssembly" when interactive

Client Services Fail During Prerender

Components in the .Client project prerender on the server. Services registered only in the client Program.cs (e.g., IWebAssemblyHostEnvironment) won't be available during prerender.

Fix by one of:

  1. Register a matching service on the server — both Program.cs files provide the service
  2. Make the service optional — use constructor injection with a nullable default: public MyComponent(IMyService? svc = null)
  3. Create a service abstraction — interface in .Client, implementations in both projects
  4. Disable prerendering for that component

Don'ts

  • Don't call JS interop in OnInitializedAsync — JS isn't available during prerender. Use OnAfterRenderAsync(firstRender).
  • Don't assume OnInitializedAsync runs once — it runs twice with prerendering. Always use [PersistentState] or ??= guards.
  • Don't use HttpContext in interactive components — it's only available during the static prerender, not during the interactive lifetime. Use [ExcludeFromInteractiveRouting] for pages that need it.
  • Don't disable prerendering as a first resort — it hurts perceived load time and SEO. Use [PersistentState] to preserve state instead.

來源與署名

來源:dotnet/skills位於plugins/dotnet-blazor/skills/support-prerendering提交0608d89

授權條款: MIT

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架