Skip to main content
skill CX Codex

support-prerendering — Claude Code skill: what it does, how to install

Configures interactive Blazor components to handle prerendering cleanly. It prevents duplicate data fetches, UI flicker during handoff, and null reference crashes.

Install into Claude Code · Skills CLI (Vercel) All methods and platforms ↓
npx skills add dotnet/skills --skill support-prerendering -a claude-code
Open on GitHub

What support-prerendering does

The skill guides the agent through interactive Blazor components in Server, WebAssembly, and Auto modes where prerendering is active by default. In this workflow, OnInitializedAsync runs twice: first to render static HTML on the server, then again when the interactive runtime connects. The guide explains how to persist state across both passes using the [PersistentState] attribute or PersistentComponentState service, eliminating screen flicker and duplicate database queries.

It covers disabling prerendering via prerender: false on component definitions, component instances, or globally in App.razor. It also details how to force static SSR with [ExcludeFromInteractiveRouting] for pages that require direct HttpContext access. In addition, the skill addresses dependencies in .Client projects that crash during server execution when missing from the server dependency container.

Calling JavaScript interop inside OnInitializedAsync fails outright because browser scripts are not running during server prerendering. Browser calls belong in OnAfterRenderAsync or behind a RendererInfo.IsInteractive check. Pure Static SSR projects do not need this skill.

What it helps with

  • eliminate duplicate data queries during Blazor prerendering
  • persist component state between server prerender and client runtime
  • stop UI flickering during the prerender-to-interactive handoff
  • disable prerendering on specific components or across the whole app
  • exclude pages from interactive routing to keep HttpContext access

Example prompts for the agent

  • My Blazor component runs OnInitializedAsync twice and duplicates queries, how do I persist state?
  • Disable prerendering on the MyChart component running with InteractiveServerRenderMode
  • Add [ExcludeFromInteractiveRouting] to the /privacy page and configure App.razor

How to install support-prerendering

1Skills CLI (Vercel)

npx skills add dotnet/skills --skill support-prerendering -a claude-code

2Manually: put the files in place

~/.claude/skills/support-prerendering/SKILL.md

Copy the whole skill folder; for one project use .claude/skills/support-prerendering/

Step by step, with commands for each platform: How to install skills in Claude Code: five ways

Questions about support-prerendering

How do I install support-prerendering in Claude Code?

Run npx skills add dotnet/skills --skill support-prerendering -a claude-code. This installs and registers the skill in Claude Code.

What does the support-prerendering skill do?

It gives the agent instructions to fix Blazor prerendering issues, including duplicate data loading, state persistence via [PersistentState], and selective prerendering deactivation.

Why does JavaScript interop fail during prerendering?

Prerendering executes statically on the server before client-side scripts start. Any JS interop call in OnInitializedAsync throws an exception and must be placed in OnAfterRenderAsync instead.

Source text

SKILL.md · MIT
---
license: MIT
name: support-prerendering
description: 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).
---

# 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**:

| Mode | Prerendering applies? |
|------|----------------------|
| None (Static SSR) | No — there's no interactive handoff |
| Server | Yes |
| WebAssembly | Yes |
| Auto | Yes |

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 · MIT · dotnet/skills

Similar skills

Everything for Frontend and UI →

From the same set101

test-analysis-extensions A reference catalog of per-language lookup tables for test analysis: markers, assertions, skips, sleep patterns. The agent reads the file matching your stack. CX Codex +1 3,100installs find-untested-sources Finds source files lacking corresponding tests using static AST parsing without project builds. Useful for coding agents before generating new test suites. CX Codex +1 3,100installs fetch-and-send-data Guides API calls and asynchronous data loading in Blazor components. Use it when configuring HttpClient, loading states, or error handling. CX Codex +1 3,100installs setup-local-sdk Installs a .NET SDK into a project-local .dotnet/ folder and wires it up through global.json, leaving the system-wide install alone. CX Codex +1 2,900installs target-authoring A skill for writing custom MSBuild targets: canonical patterns plus fixes for anti-patterns like overwriting DependsOn chains. CX Codex +1 3,000installs template-comparison This skill puts two or more dotnet new templates side by side in a table and picks a winner for your scenario. Handy when you are stuck between webapi and webapp or blazor and blazorwasm. CX Codex +1 2,800installs template-smart-defaults A skill that fills related dotnet new parameters when you leave them unset, and leaves your explicit choices alone. CX Codex +1 2,800installs migrate-xunit-to-mstest Converts .NET test suites from xUnit.net v2 or v3 to MSTest v4 while preserving VSTest or MTP runners. Helpful when unifying test frameworks across .NET repositories. CX Codex +1 2,900installs

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor