In a Blazor Web App, what does [StreamRendering] change when a statically rendered page awaits slow data in OnInitializedAsync?
answer
- static SSR or prerendering only
- placeholder first, data later
- same response, patched by blazor.web.js
- inherited by child components
- [StreamRendering(false)] turns a subtree off
basics
~20 sWithout it the server waits for the page's async work before sending any HTML. With [StreamRendering] it sends the first render (for example a loading message) at once, then streams the finished markup later in the same response, which blazor.web.js patches into place.
solid answer
~40 sUnder **static SSR** (and during prerendering) Blazor normally waits until every component's async initialization completes before writing the response, so a slow query delays the whole page. `@attribute [StreamRendering]` on the component lets the server flush the first render - typically a `Loading...` branch - immediately, keep the response open, and when the awaited task finishes, append the updated markup as `<blazor-ssr>` chunks at the end of the same response; `blazor.web.js` moves them into the right place. It is opt-in because content can shift. Children without the attribute inherit their parent's setting, `[StreamRendering(false)]` turns it off for a subtree, and the attribute has no effect on interactive rendering. Since .NET 9 the parameterless form means `true`; .NET 8 code wrote `[StreamRendering(true)]`.
code
razor · 19 lines@page "/payslips"
@attribute [StreamRendering]
@inject IPayslipService Payslips
@if (items is null)
{
<p>Loading payslips...</p>
}
else
{
@foreach (var p in items) { <p>@p.Month: @p.Net</p> }
}
@code {
private IReadOnlyList<PayslipRow>? items;
protected override async Task OnInitializedAsync()
=> items = await Payslips.GetForCurrentUserAsync();
}go deeper
Know that a static Blazor page normally waits for its data before anything is sent, and that [StreamRendering] lets a loading message appear first.
Explain how the first render is flushed and later updates are appended to the same response and patched by blazor.web.js, plus inheritance and the false opt-out.
Decide where streaming helps: slow sections on static pages, placeholder sizing to avoid layout shift, work that must finish before headers are sent, and proxies that buffer responses.
Use streaming as part of a static-first page strategy, so slow data does not push teams into making whole pages interactive just to show a spinner.
## The problem it solves In a **Blazor Web App**, a page with no interactive render mode is rendered with **static server-side rendering (static SSR)**: the server runs the components once and writes their HTML to the HTTP response. If a component awaits slow work in `OnInitializedAsync` - an HR database query, a call to a payroll service - the default behaviour is to **wait for that task** before sending the page, so the user stares at a blank tab. Prerendering of interactive components behaves the same way. ## What the attribute does `StreamRenderingAttribute` (namespace `Microsoft.AspNetCore.Components`) is applied with `@attribute [StreamRendering]`. When a component carries it, Blazor's endpoint renderer: 1. renders the page as far as it can without waiting - an `if (data is null)` branch produces a placeholder; 2. flushes that HTML to the browser straight away, with boundary markers around the streaming component; 3. keeps the response open while the component's task runs; 4. when the component re-renders, writes the new markup at the end of the same response inside a `<blazor-ssr>` element, as a `<template>` tagged with the component's id; 5. lets `blazor.web.js` move that content into the marked location in the DOM. There is no second HTTP request and no circuit: it is still static SSR, just delivered in pieces. The template's sample `Weather` page uses this pattern when interactivity is per page. ## The rules | Rule | Detail | |---|---| | Where it works | static SSR and prerendering; interactive rendering ignores it | | Default | off; a component opts in, because streamed updates can make content shift | | Inheritance | a component without the attribute takes its parent's setting | | Opting out | `[StreamRendering(false)]` turns streaming off for that component and its subtree | | Constructor | `StreamRenderingAttribute(bool enabled = true)`; `Enabled` exposes the value | | Buffering hosts | if something buffers the response, the page still loads, just without streaming | ## When it helps and when it does not Good fits: - a static portal page with one slow section, such as a payslip history, where everything else is ready at once; - prerendered interactive pages whose first data load is slow. Poor fits or limits: - **Interactive components** already render asynchronously in their own runtime; the attribute does nothing there. - **Fast data.** If the await is short, streaming only adds a placeholder flash; render normally. - **Layout shift.** A placeholder much smaller than the final content makes the page jump, which is why streaming is opt-in. Size the placeholder like the final content. - **Response already started.** Once the first chunk is flushed, the status code and headers are sent, so work that might need to change them is better done before the first await. - **Intermediaries that buffer** (some proxies or compression set-ups) remove the benefit even though the page still works. ## .NET 8, 9 and 10 Streaming rendering arrived with Blazor Web Apps in .NET 8, where samples wrote `[StreamRendering(true)]`. From .NET 9 the parameterless `[StreamRendering]` is the documented form. In .NET 10, streamed rendering also cooperates with the new Not Found handling: `NavigationManager.NotFound()` during a streamed render can show the router's `NotFoundPage` without a reload when enhanced navigation is active - a routing topic, but one reason to prefer a routable Not Found page over the obsolete `NotFound` fragment.
- A component inside a streaming page must not appear in a half-loaded state. How do you exclude it?Put `@attribute [StreamRendering(false)]` on that component. Streaming is inherited down the tree, and passing `false` turns it off for that component and everything below it, so its markup is only written once its own work has completed.
- Does [StreamRendering] make the streamed section interactive once the data arrives?No. Streaming changes only how static HTML is delivered; the streamed markup is still static SSR output with no event handlers behind it. Interactivity needs an interactive render mode on that component.
- Streaming rendering is on by default for every static SSR page in .NET 10.
- [StreamRendering] opens a SignalR connection to push the remaining content.
- [StreamRendering] makes the streamed section interactive once its data arrives.
- Each streamed update is fetched by the browser with a separate HTTP request.
- Child components need their own [StreamRendering] attribute or they block the stream.
Assumes .NET 10; since .NET 9 [StreamRendering] needs no argument (the constructor defaults enabled to true), while .NET 8 samples wrote [StreamRendering(true)].