skip to content

In a Blazor Web App, what does prerendering do to an interactive component, what problems follow, and how do you detect or disable it?

level: middleimportance: must knowfreq 62%

answer

  1. on by default for all interactive modes
  2. the component is created twice
  3. RendererInfo.IsInteractive is false while prerendering
  4. new InteractiveServerRenderMode(prerender: false)
  5. client-only services fail on the server

basics

~20 s

Prerendering renders an interactive component to HTML on the server first, so content appears before interactivity starts. The component is then created again when it becomes interactive, which doubles initialization and breaks client-only services; detect it with RendererInfo, disable it with prerender: false.

solid answer

~40 s

Interactive Server, WebAssembly and Auto all **prerender by default**: the server renders the component's HTML into the response, then a second instance is created when the circuit or the WebAssembly runtime starts and replaces that markup. That gives fast first paint, but the component initializes twice, state built during prerendering is lost unless persisted, buttons look clickable before they work, and a `.Client` component that injects a browser-only service such as `IWebAssemblyHostEnvironment` fails on the server. Since .NET 9, `RendererInfo.IsInteractive` is `false` while prerendering and `AssignedRenderMode` is non-null, which separates prerendering from static SSR. Disable it with `new InteractiveServerRenderMode(prerender: false)` (or the WebAssembly / Auto equivalent) on the top-level component that sets the mode; a child's `prerender` flag is ignored under a parent that already set one.

code

razor · 15 lines
razor
@page "/leave/new"
@rendermode InteractiveServer

<button disabled="@(!RendererInfo.IsInteractive)" @onclick="Submit">
    Submit request
</button>

@if (AssignedRenderMode is not null && !RendererInfo.IsInteractive)
{
    <p>Preparing the form...</p>
}

@code {
    private Task Submit() => Task.CompletedTask;
}

go deeper

for a junior

Know that interactive components show server-rendered HTML first and become clickable a moment later, and that prerendering is on by default.

for a middle

Explain the two component instances, why initialization runs twice, how RendererInfo and AssignedRenderMode tell prerendering from static SSR, and how the prerender flag is set.

for a senior

Diagnose double data loads, flicker and client-only service failures as prerendering effects, and decide per component whether to persist state, disable buttons until interactive, or switch prerendering off.

for a principal

Weigh first-paint speed against duplicated work and the dead window across the app, and define where prerendering is turned off so the choice is consistent rather than per developer.

## What prerendering is in Blazor In a **Blazor Web App**, a component with an interactive render mode - `InteractiveServer`, `InteractiveWebAssembly` or `InteractiveAuto` - does not wait for interactivity before showing anything. By default the server first **prerenders** it: it runs the component on the server with the same static HTML renderer used for static SSR, writes the resulting markup into the HTTP response, and adds a marker telling `blazor.web.js` where an interactive component belongs. When the circuit connects (Server) or the .NET WebAssembly runtime starts (WebAssembly), a **new component instance** is created in that runtime, renders again, and takes over that part of the DOM. The flag lives on the render mode objects: `InteractiveServerRenderMode`, `InteractiveWebAssemblyRenderMode` and `InteractiveAutoRenderMode` each expose a `Prerender` property, and their parameterless constructors pass `true`. The shorthand `RenderMode.InteractiveServer` and friends use those constructors, so prerendering is on unless you construct a mode yourself. ## What it buys and what it costs Benefits: - the user sees real content in the first response instead of an empty shell; - crawlers and link previews get HTML (less relevant for an internal portal); - nothing about the page depends on the WebAssembly download finishing before content appears. Costs: - **Double work.** The component is created and initialized twice - once during prerendering, once when interactive - so a data load in its initialization method runs twice unless the prerendered state is persisted (`[PersistentState]` in .NET 10 handles that). - **Flicker.** If the interactive instance reloads data asynchronously, the prerendered markup can be replaced by a loading state and then by the data again. - **A dead window.** Between first paint and interactivity, buttons are visible but their handlers cannot run. - **Wrong-host services.** A component in the `.Client` project is still prerendered on the server, so injecting a service registered only in the client's `Program.cs` - `IWebAssemblyHostEnvironment` is the classic case - fails during prerendering with an error that no service of that type is registered. - **After-render code waits.** `OnAfterRender{Async}` does not run during prerendering, only once the component renders interactively. ## Detecting prerendering at runtime (.NET 9 and later) `ComponentBase` exposes two properties: | Situation | `RendererInfo.Name` | `RendererInfo.IsInteractive` | `AssignedRenderMode` | |---|---|---|---| | Static SSR | `Static` | `false` | `null` | | Prerendering an interactive component | `Static` | `false` | the assigned mode, for example `InteractiveServerRenderMode` | | Running interactively on the server | `Server` | `true` | `InteractiveServerRenderMode` | | Running interactively in the browser | `WebAssembly` | `true` | `InteractiveWebAssemblyRenderMode` | So `!RendererInfo.IsInteractive` covers both "not interactive yet" and "never interactive", while `AssignedRenderMode is null` singles out static SSR. A common pattern is `disabled="@(!RendererInfo.IsInteractive)"` on a submit button so nobody clicks it before the handler can run. The source also shows a detail the docs table glosses over: once interactive, the server renderer reports `InteractiveServerRenderMode` and the WebAssembly renderer reports `InteractiveWebAssemblyRenderMode`, even for a component that was assigned Auto. ## Disabling it 1. **One instance:** `<OrgChart @rendermode="new InteractiveWebAssemblyRenderMode(prerender: false)" />`. 2. **One component definition:** `@rendermode @(new InteractiveServerRenderMode(prerender: false))`. 3. **The whole app:** set the mode with `prerender: false` on `<Routes>` and `<HeadOutlet>` in `App.razor`. The setting only counts where a render mode boundary starts. If a parent already set a render mode, a child's `prerender` flag is ignored. Turning prerendering off trades the problems above for an empty area until interactivity starts, so it suits widgets that cannot render meaningfully on the server, not pages users land on.

  • A .Client component injects IWebAssemblyHostEnvironment and the page fails on first load only. Why, and what are the fixes?
    The component is prerendered on the server, where that client-only service is not registered, so property injection fails. Fixes: register an equivalent service on the server too, make the dependency optional (an optional constructor parameter or `IServiceProvider.GetService`), hide it behind your own abstraction with a server and a client implementation, or disable prerendering for that component.
  • Why does setting prerender: false on a child component inside an InteractiveServer page have no effect?
    Prerendering is decided where a render mode boundary begins. The page already set `InteractiveServer` with prerendering on, so the child is part of that interactive subtree and is prerendered with it; its own `prerender` flag is ignored. To change it, set the flag on the component that establishes the mode - the page, or `Routes` for global interactivity.

  • Prerendering is off by default and must be switched on with a flag on each interactive component.
  • The prerendered component instance becomes the interactive one, so its fields are still set afterwards.
  • RendererInfo.Name returns Server while an InteractiveServer component is being prerendered.
  • Setting prerender: false on any child component disables prerendering for that child.
  • OnAfterRenderAsync runs during prerendering, so JavaScript can be called there.

Assumes .NET 10; RendererInfo and AssignedRenderMode on ComponentBase arrived in .NET 9, so .NET 8 code detected prerendering by other means.