In a Blazor Web App, where does a component's in-memory state live on Interactive Server and on WebAssembly, and what survives a browser refresh?
answer
- server: circuit memory on the server
- WebAssembly: memory of the browser tab
- static SSR: nothing between requests
- refresh = new circuit or new app
- persist what users worked to create
basics
~20 sOn Interactive Server, component fields and circuit-scoped services live in server memory inside the user's circuit; on WebAssembly they live in the browser tab's .NET runtime. A full refresh starts a new circuit or app, so only data saved elsewhere survives.
solid answer
~40 sBlazor keeps state as ordinary .NET objects: component fields and properties, plus scoped services. On **Interactive Server** they live in the server process inside the user's **circuit**, the per-tab session reached over a SignalR connection. On **Interactive WebAssembly** they live in the .NET runtime running in that browser tab. Navigating between interactive pages keeps those services alive, but a full page refresh, closing the tab or opening a new one creates a new circuit or a new WebAssembly app with freshly initialized state. A statically rendered component keeps nothing between requests. Anything users spend effort on, such as a cake-order basket, must therefore be saved somewhere that outlives the instance: a database, browser storage such as `ProtectedLocalStorage`, or the URL.
code
razor · 13 lines@page "/order"
@rendermode InteractiveServer
<p>@lines.Count cake(s) in this order</p>
<button @onclick="AddEclair">Add an eclair</button>
@code {
// Lives in this component instance, inside the user's circuit on the server.
// A full page refresh creates a new circuit and this list starts empty again.
private readonly List<string> lines = [];
private void AddEclair() => lines.Add("Chocolate eclair");
}go deeper
Recall that Interactive Server state is in server memory per circuit, WebAssembly state is in the browser tab, and a refresh loses both.
Explain the circuit, why reconnects can still lose state, and which storage fits each kind of state that must survive.
Size per-circuit memory, decide what is persisted where, and design for reconnects to a different server in a farm.
Choose render modes partly on state cost: server memory per tab versus browser-held state and what each does to scale.
## State is just objects, held somewhere Blazor has no special state store. The state of a page is the values of fields and properties in **component instances** and in **services** resolved from dependency injection. The question is where those objects live, and that depends on how the component is rendered. | Rendering | Where the objects live | How long | |---|---|---| | Static SSR (no render mode) | Server, for one HTTP request | Until the response is written | | Interactive Server | Server, inside the user's **circuit** | Until the circuit ends | | Interactive WebAssembly | The browser tab's .NET runtime | Until the tab unloads the app | | Interactive Auto | Server first, then WebAssembly once downloaded | Per the active runtime | ## The circuit On Interactive Server, each browser tab that runs interactive components gets a **circuit**: the server-side session that holds the component tree, its last render output, the fields of every component instance, and the DI services scoped to that circuit. The browser talks to it over a SignalR connection. User state on the server is therefore kept in server memory, per tab. If the network drops briefly, Blazor tries to reconnect the browser to the same circuit, and the state is still there. If reconnection is impossible, because the server released the circuit, the server restarted, or another server in a farm received the reconnect, the user gets a **new circuit with newly initialized state**, like closing and reopening a desktop app. ## WebAssembly With Interactive WebAssembly, the .NET runtime and the components run inside the browser tab. State lives in that tab's memory. Nothing on the server knows about it, and nothing is lost when the server restarts, but it is gone when the page reloads. ## What a refresh does - **Navigation between interactive pages** inside the app keeps the circuit or WebAssembly app alive, so scoped services such as a basket keep their contents. - **A full reload** (F5, a `forceLoad` navigation, or opening the URL in a new tab) starts a fresh circuit or a fresh WebAssembly app. In-memory state is gone. - **Closing the tab** ends the circuit or app. - Values set in JavaScript variables through JS interop are also lost on reload. ## Deciding what to persist Persistence is never automatic; the app chooses what to keep and where. The documentation's rule of thumb is to persist state users expended effort to create, not data that can simply be read again. 1. **High-value, cross-device state** such as a submitted cake order or a saved basket: store it server-side, typically in a database keyed by the user. 2. **Per-browser convenience state** such as an unsaved basket for an anonymous visitor: browser storage, with `ProtectedLocalStorage` or `ProtectedSessionStorage` on Interactive Server. 3. **Navigation state** such as the selected cake category or page number: the URL, which also survives bookmarks and sharing. 4. **Prerendered data** loaded once on the server: `[PersistentState]` hands it to the interactive instance so it is not loaded twice. UI structure itself, such as component instances and their render trees, cannot be persisted; model the UI state you care about, for example which order panel is expanded, as serializable data. ## Misconceptions to unlearn - **"Blazor Server runs in the browser."** Only the rendered HTML and a small script run there; components and their fields live on the server. - **"Scoped means per user."** On the server it means per circuit, which is per tab; two tabs of the same user have two baskets. - **"Reconnecting always restores everything."** Only while the server still holds the circuit; after that, only state the app explicitly persisted comes back. - **"WebAssembly state is safe from server restarts, so it is durable."** It survives server restarts but not a reload of the tab. - **"Static pages keep their fields between posts."** Each request creates fresh component instances. ## Consequences for server memory Because every connected tab holds its state on the server, per-user memory adds up: a basket that caches full product images for thousands of open tabs is a server capacity problem, not a browser one. Keep circuit state small and load large data on demand.
- A user's basket survives clicking between pages of the patisserie app but is empty after pressing F5; why?In-app navigation between interactive pages keeps the same circuit or WebAssembly app, so a scoped basket service keeps its contents. F5 is a full reload: the browser starts a new circuit or a new WebAssembly app with fresh services. To survive it, save the basket to the server or to browser storage and reload it on startup.
- Why can server memory become a problem with Interactive Server state even when each user's state is small?Each open tab holds a circuit with its component tree and scoped services in server memory, and disconnected circuits are kept for a while in case the user returns. Multiply small per-user state by thousands of tabs and it becomes the server's capacity limit, so circuit state should stay lean.
- Interactive Server components keep their state in the browser.
- Component state on Interactive Server survives a full page refresh.
- Blazor persists component fields automatically between visits.
- A statically rendered component keeps its fields between form posts.
- Opening the app in a second tab shares the first tab's circuit.