# Server Rendering

Course: [Blazor](https://www.devtutorials.ai/learn/blazor) · Section: Render Modes
Source: https://www.devtutorials.ai/learn/blazor/render-modes/server-rendering

> Server render modes in Blazor: the four render modes, Static SSR with streaming and enhanced navigation, Interactive Server with @rendermode, SignalR and circuits, reconnection, component and app state, prerendering and PersistentComponentState.

## Render Modes Overview

- A **render mode** says **where** a component runs and whether it can react to clicks.
- **Interactive** means the page reacts to the user: clicks, typing and live updates run your C# code.
- Blazor has four render modes:

<div className="tscroll">

| Render mode | Where the C# runs | Interactive? |
|---|---|---|
| **Static SSR** | On the server, once per page request | No |
| **Interactive Server** | On the server, over a live connection | Yes |
| **Interactive WebAssembly** | In the browser | Yes |
| **Interactive Auto** | On the server first, then in the browser | Yes |

</div>

- This lesson covers the two **server** modes. The next lesson, **WebAssembly & Auto**, covers the other two.

**Real-World Example:**

- Ravi's **Menu** page only shows teas and prices. It does not need clicks, so it can be static.
- His **Order** page has + and − buttons and a live total. It must be interactive.
- One Blazor app can use both: each page picks its own render mode.

<figure className="blz-fig">
<svg viewBox="0 0 640 190" role="img" aria-labelledby="fig-rs-modes">
  <title id="fig-rs-modes">Four cards for the render modes. Static SSR: server icon, no clicks. Interactive Server: server icon with a live line to the browser. Interactive WebAssembly: browser icon. Interactive Auto: server then browser. The first two are highlighted as this lesson.</title>
  <defs><marker id="fig-rs-modes-a" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto"><path d="M0 0 L10 5 L0 10 z" style={{ fill: "var(--muted)" }} /></marker></defs>
  <rect className="pur" x="10" y="30" width="145" height="120" rx="10" />
  <text className="t m" x="82" y="58" textAnchor="middle" style={{ fontSize: 12, fontWeight: 700 }}>Static SSR</text>
  <text className="t" x="82" y="96" textAnchor="middle" style={{ fontSize: 20 }}>🖥️</text>
  <text className="t" x="82" y="128" textAnchor="middle" style={{ fontSize: 10 }}>server, no clicks</text>
  <rect className="pur" x="165" y="30" width="145" height="120" rx="10" />
  <text className="t m" x="237" y="50" textAnchor="middle" style={{ fontSize: 12, fontWeight: 700 }}>Interactive</text>
  <text className="t m" x="237" y="66" textAnchor="middle" style={{ fontSize: 12, fontWeight: 700 }}>Server</text>
  <text className="t" x="237" y="96" textAnchor="middle" style={{ fontSize: 20 }}>🖥️⇄🌐</text>
  <text className="t" x="237" y="128" textAnchor="middle" style={{ fontSize: 10 }}>server, live link</text>
  <rect className="box" x="320" y="30" width="145" height="120" rx="10" />
  <text className="t m" x="392" y="50" textAnchor="middle" style={{ fontSize: 12, fontWeight: 700 }}>Interactive</text>
  <text className="t m" x="392" y="66" textAnchor="middle" style={{ fontSize: 12, fontWeight: 700 }}>WebAssembly</text>
  <text className="t" x="392" y="96" textAnchor="middle" style={{ fontSize: 20 }}>🌐</text>
  <text className="t" x="392" y="128" textAnchor="middle" style={{ fontSize: 10 }}>in the browser</text>
  <rect className="box" x="475" y="30" width="155" height="120" rx="10" />
  <text className="t m" x="552" y="50" textAnchor="middle" style={{ fontSize: 12, fontWeight: 700 }}>Interactive</text>
  <text className="t m" x="552" y="66" textAnchor="middle" style={{ fontSize: 12, fontWeight: 700 }}>Auto</text>
  <text className="t" x="552" y="96" textAnchor="middle" style={{ fontSize: 20 }}>🖥️ → 🌐</text>
  <text className="t" x="552" y="128" textAnchor="middle" style={{ fontSize: 10 }}>server first, then browser</text>
  <text className="t m" x="160" y="175" textAnchor="middle" style={{ fontSize: 12 }}>This lesson</text>
  <text className="t m" x="475" y="175" textAnchor="middle" style={{ fontSize: 12 }}>Next lesson</text>
</svg>
<figcaption>The four render modes. This lesson covers the two server modes.</figcaption>
</figure>

- A component can ask which mode it is running in: `RendererInfo.Name` gives the name, and `RendererInfo.IsInteractive` says if clicks work.

In the below example, `Home.razor` shows its own render mode. The code boxes on this site run in your browser, so they say **WebAssembly**.

<p>Interactive: @RendererInfo.IsInteractive</p>
`,
  }}
/>

- In a Static SSR page, the same code shows **Static** and **False**. In an Interactive Server page, it shows **Server** and **True**.

## Static SSR

- **SSR** means **server-side rendering**: the server runs your component and makes the HTML.
- **Static** means the page is sent once and then stays as it is. Clicks do not run C# code.
- It is the default for pages that have no `@rendermode`.

### Project Setup

- `--interactivity None` makes a Blazor Web App where every page is Static SSR.

`Program.cs` (shortened) only adds Razor components, with no interactive lines:

### Rendering Flow

- The browser asks for a page, for example `/menu`.
- The server runs the component, makes the HTML and sends it.
- The browser shows the HTML. The connection is then **closed**. Nothing on the server keeps running for this page.

<figure className="blz-fig">
<svg viewBox="0 0 640 180" role="img" aria-labelledby="fig-rs-ssr">
  <title id="fig-rs-ssr">Animation: a request for /menu goes from the browser to the server. The server runs Menu.razor and sends back HTML. The browser shows the menu. The line between them then fades: no connection stays open.</title>
  <defs><marker id="fig-rs-ssr-a" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto"><path d="M0 0 L10 5 L0 10 z" style={{ fill: "var(--muted)" }} /></marker></defs>
  <rect className="blu" x="20" y="40" width="190" height="120" rx="10" />
  <text className="t m" x="115" y="66" textAnchor="middle" style={{ fontSize: 13, fontWeight: 700 }}>Browser</text>
  <text className="t" x="115" y="100" textAnchor="middle" style={{ fontSize: 12 }}>Menu: Masala ₹20<animate attributeName="opacity" values="0;0;1;1" keyTimes="0;0.6;0.65;1" dur="5s" repeatCount="indefinite" /></text>
  <rect className="pur" x="430" y="40" width="190" height="120" rx="10" />
  <text className="t m" x="525" y="66" textAnchor="middle" style={{ fontSize: 13, fontWeight: 700 }}>Server</text>
  <text className="t" x="525" y="100" textAnchor="middle" style={{ fontSize: 12 }}>runs Menu.razor</text>
  <text className="t" x="525" y="120" textAnchor="middle" style={{ fontSize: 12 }}>makes HTML</text>
  <g>
  <animate attributeName="opacity" values="1;1;0.15;0.15" keyTimes="0;0.7;0.85;1" dur="5s" repeatCount="indefinite" />
  <path className="line" d="M210 80 H426" markerEnd="url(#fig-rs-ssr-a)" />
  <path className="line" d="M430 130 H214" markerEnd="url(#fig-rs-ssr-a)" />
  <text className="t" x="320" y="72" textAnchor="middle" style={{ fontSize: 11 }}>GET /menu</text>
  <text className="t" x="320" y="150" textAnchor="middle" style={{ fontSize: 11 }}>HTML</text>
  </g>
  <circle r="7" style={{ fill: "#7c3aed" }}>
    <animate attributeName="cx" values="210;426;426;214;214" keyTimes="0;0.25;0.35;0.6;1" dur="5s" repeatCount="indefinite" />
    <animate attributeName="cy" values="80;80;130;130;130" keyTimes="0;0.25;0.35;0.6;1" dur="5s" repeatCount="indefinite" />
  </circle>
</svg>
<figcaption>Static SSR: one request, one HTML answer, then the connection closes.</figcaption>
</figure>

### Streaming Rendering

- Some pages load slow data, like orders from a database. Without streaming, the user sees nothing until the data is ready.
- `@attribute [StreamRendering]` sends the page **at once**, with "Loading...". When the data is ready, the server sends the rest on the **same** response.
- It is still Static SSR: no clicks, just a faster first view.

In the below example, `OnInitializedAsync` waits two seconds for the orders. The page shows "Loading..." first, then the list.

@if (orders == null)
{
      <p>Loading...</p>
}
else
{
      <ul>
          @foreach (var o in orders)
          {
              <li>@o</li>
          }
      </ul>
}

@code {
      private string[]? orders;

      protected override async Task OnInitializedAsync()
      {
          await Task.Delay(2000); // pretend to read the database
          orders = new[] { "Asha: 2 Masala Tea", "Ravi: 1 Ginger Tea" };
      }
}
`,
  }}
/>

### Enhanced Navigation

- In a normal website, every link click loads a **whole new page**. The screen flashes, and scroll position is lost.
- Blazor's script (`blazor.web.js`) adds **enhanced navigation**. It catches the link click, fetches the new page in the background and **only changes the parts that differ**.
- It feels like a single-page app, even with Static SSR. Form posts get the same treatment.
- To turn it off for one link, add `data-enhance-nav="false"`.

<figure className="blz-fig">
<svg viewBox="0 0 640 115" role="img" aria-labelledby="fig-rs-enh">
  <title id="fig-rs-enh">Animation, two rows. Top row, normal navigation: the whole page goes white and reloads. Bottom row, enhanced navigation: the header and menu stay, and only the middle part changes from Menu to Order.</title>
  <defs><marker id="fig-rs-enh-a" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto"><path d="M0 0 L10 5 L0 10 z" style={{ fill: "var(--muted)" }} /></marker></defs>
  <text className="t m" x="20" y="24" style={{ fontSize: 12, fontWeight: 700 }}>Normal link</text>
  <rect className="box" x="20" y="34" width="290" height="70" rx="8"><animate attributeName="opacity" values="1;0.1;1;1" keyTimes="0;0.4;0.6;1" dur="4s" repeatCount="indefinite" /></rect>
  <text className="t" x="165" y="74" textAnchor="middle" style={{ fontSize: 12 }}>Whole page reloads<animate attributeName="opacity" values="1;0;1;1" keyTimes="0;0.4;0.6;1" dur="4s" repeatCount="indefinite" /></text>
  <text className="t m" x="330" y="24" style={{ fontSize: 12, fontWeight: 700 }}>Enhanced navigation</text>
  <rect className="pur" x="330" y="34" width="290" height="22" rx="6" />
  <text className="t m" x="475" y="50" textAnchor="middle" style={{ fontSize: 11 }}>☕ Ravi's Tea Shop (stays)</text>
  <rect className="grn" x="330" y="60" width="290" height="44" rx="6" />
  <text className="t m" x="475" y="87" textAnchor="middle" style={{ fontSize: 12 }}>Menu page<animate attributeName="opacity" values="1;1;0;0" keyTimes="0;0.45;0.5;1" dur="4s" repeatCount="indefinite" /></text>
  <text className="t m" x="475" y="87" textAnchor="middle" style={{ fontSize: 12 }} opacity="0">Order page<animate attributeName="opacity" values="0;0;1;1" keyTimes="0;0.45;0.5;1" dur="4s" repeatCount="indefinite" /></text>
</svg>
<figcaption>Enhanced navigation keeps the page and changes only what is different.</figcaption>
</figure>

@* This link always does a full page load *@
<a href="pdf/price-list.pdf" data-enhance-nav="false">Price list (PDF)</a>
`}
/>

### Use Cases and Limitations

<div className="tscroll">

| Good for | Not possible |
|---|---|
| Pages that only show content: menu, blog, About | `@onclick` and other events do not run |
| Fast first view, small pages | No live updates, like a ticking timer |
| Search engines can read every page | `@bind` does not update while typing |
| Forms that post to the server | Each change needs a new request |

</div>

In a Static SSR page, a button with `@onclick` still shows, but clicking it does nothing. This is the most common surprise for beginners. Give the page an interactive render mode when it needs clicks.

## Interactive Server

- **Interactive Server** runs your components **on the server**, but the page stays connected to it.
- A click in the browser travels to the server, your C# runs there, and the server sends back what changed.
- So clicks, `@bind` and timers all work, and your C# code never goes to the browser.

### Project Setup

- `--interactivity Server` is the default for `dotnet new blazor`. You used it in **Getting Started**.
- `--all-interactive` makes **every** page interactive. Without it, each page chooses.

`Program.cs` (shortened) gets two extra lines, one for the services and one for the endpoint:

### Render Modes (@rendermode)

You choose the render mode in one of three places:

- **One page:** put `@rendermode InteractiveServer` at the top of the page.
- **One component:** write it on the tag, like `<OrderBox @rendermode="InteractiveServer" />`. The rest of the page stays static.
- **The whole app:** put it on `<Routes />` and `<HeadOutlet />` in `App.razor`.

<p>Cups: @cups</p>

@code {
      private int cups;
      private void AddCup() => cups++;
}
`}
/>

...
`}
/>

The code boxes on this site are always interactive, so their examples leave out `@rendermode`. In your own project, add it to every page that needs clicks.

### SignalR and Circuits

- **SignalR** is the live, two-way connection between the browser and the server. It usually uses a **WebSocket**: a connection that stays open.
- A **circuit** is the server's memory for **one browser tab**: the components of that tab and their values.
- Each open tab has its own circuit. Closing the tab ends it.

<figure className="blz-fig">
<svg viewBox="0 0 640 195" role="img" aria-labelledby="fig-rs-circuit">
  <title id="fig-rs-circuit">Animation: two browser tabs, Asha and Ravi. Each has a live SignalR line to the server. On the server, each tab has its own circuit box with its own cups value: Asha 2, Ravi 5. A click dot travels along the line from the first tab.</title>
  <defs><marker id="fig-rs-circuit-a" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto"><path d="M0 0 L10 5 L0 10 z" style={{ fill: "var(--muted)" }} /></marker></defs>
  <rect className="blu" x="20" y="25" width="150" height="55" rx="10" />
  <text className="t m" x="95" y="57" textAnchor="middle" style={{ fontSize: 12 }}>Asha's tab</text>
  <rect className="blu" x="20" y="115" width="150" height="55" rx="10" />
  <text className="t m" x="95" y="147" textAnchor="middle" style={{ fontSize: 12 }}>Ravi's tab</text>
  <rect className="box" x="330" y="10" width="290" height="175" rx="10" />
  <text className="t m" x="475" y="32" textAnchor="middle" style={{ fontSize: 12, fontWeight: 700 }}>Server</text>
  <rect className="pur" x="370" y="42" width="210" height="50" rx="8" />
  <text className="t m" x="475" y="72" textAnchor="middle" style={{ fontSize: 12 }}>Circuit 1: cups = 2</text>
  <rect className="pur" x="370" y="122" width="210" height="50" rx="8" />
  <text className="t m" x="475" y="152" textAnchor="middle" style={{ fontSize: 12 }}>Circuit 2: cups = 5</text>
  <path className="line" d="M170 52 H366" style={{ strokeDasharray: "6 4" }} />
  <path className="line" d="M170 142 H366" style={{ strokeDasharray: "6 4" }} />
  <text className="t" x="268" y="44" textAnchor="middle" style={{ fontSize: 11 }}>SignalR</text>
  <text className="t" x="268" y="134" textAnchor="middle" style={{ fontSize: 11 }}>SignalR</text>
  <circle r="6" cy="52" style={{ fill: "#7c3aed" }}>
    <animate attributeName="cx" values="170;366;170" dur="3s" repeatCount="indefinite" />
  </circle>
</svg>
<figcaption>Every tab has a live SignalR line and its own circuit on the server.</figcaption>
</figure>

### UI Updates and Reconnection

- After a click, the server renders again, compares the new HTML with the old one, and sends **only the changes**. The browser patches the page.
- That is why each click needs a quick trip to the server.

In the below example, each click adds a cup. In an Interactive Server app, each click goes to the server, and only the number and the total come back.

<p>Cups: @cups</p>
<p>Total: ₹@(cups * 20)</p>

@code {
      private int cups;

      private void AddCup() => cups++;
}
`,
  }}
/>

- **Reconnection:** if the connection drops (for example, Wi-Fi is lost), Blazor shows a box saying "Rejoining the server..." and tries again.
- If it reconnects in time, the circuit is still there and nothing is lost.
- If the server has already removed the circuit, the user must reload the page. In .NET 10, a circuit can also be **paused** and **resumed** later, so its state survives.
- The box comes from `Components/Layout/ReconnectModal.razor` in the template. You can change its text and style.

<figure className="blz-fig">
<svg viewBox="0 0 640 190" role="img" aria-labelledby="fig-rs-reconnect">
  <title id="fig-rs-reconnect">Animation: the SignalR line between browser and server breaks. A box appears in the browser: Rejoining the server... Then the line comes back and the box disappears.</title>
  <defs><marker id="fig-rs-reconnect-a" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto"><path d="M0 0 L10 5 L0 10 z" style={{ fill: "var(--muted)" }} /></marker></defs>
  <rect className="blu" x="20" y="30" width="230" height="130" rx="10" />
  <text className="t m" x="135" y="54" textAnchor="middle" style={{ fontSize: 12, fontWeight: 700 }}>Browser</text>
  <rect x="45" y="80" width="180" height="50" rx="8" style={{ fill: "#fff7ed", stroke: "#ea580c" }}><animate attributeName="opacity" values="0;0;1;1;0" keyTimes="0;0.3;0.35;0.75;0.8" dur="5s" repeatCount="indefinite" /></rect>
  <text className="t" x="135" y="110" textAnchor="middle" style={{ fontSize: 11 }}>Rejoining the server...<animate attributeName="opacity" values="0;0;1;1;0" keyTimes="0;0.3;0.35;0.75;0.8" dur="5s" repeatCount="indefinite" /></text>
  <rect className="pur" x="420" y="30" width="200" height="130" rx="10" />
  <text className="t m" x="520" y="54" textAnchor="middle" style={{ fontSize: 12, fontWeight: 700 }}>Server</text>
  <text className="t" x="520" y="100" textAnchor="middle" style={{ fontSize: 12 }}>circuit kept</text>
  <path className="line" d="M250 95 H416" style={{ strokeDasharray: "6 4" }}><animate attributeName="opacity" values="1;1;0;0;1" keyTimes="0;0.3;0.32;0.75;0.78" dur="5s" repeatCount="indefinite" /></path>
  <text className="t" x="333" y="86" textAnchor="middle" style={{ fontSize: 11 }}>SignalR</text>
</svg>
<figcaption>A lost connection shows the reconnect box until the line is back.</figcaption>
</figure>

### Use Cases and Limitations

<div className="tscroll">

| Good for | Limits |
|---|---|
| Fast first start: nothing big to download | Needs a live connection all the time |
| Code and secrets stay on the server | Every click waits for a trip to the server |
| Easy access to the database and server files | Each user's circuit uses server memory |
| Office and business apps on a good network | Does not work offline |

</div>

## State Management

- **State** is the data your app remembers right now: the cups, the cart, the user's name.
- Managing state means deciding **where** each piece of data lives and **how long** it lives.

### Component and App State

- **Component state** lives in a component's fields, like `private int cups;`. It is lost when the component is removed, or when the page is reloaded.
- **App state** is data that many components need, like the shopping cart. It must live **outside** a single component, for example in the parent page that passes it down with parameters.

In the below example, `CupPanel.razor` keeps `cups` as component state. Click **Add cup**, then **Hide** and **Show**: the count starts from 0 again.

@if (show)
{
}

@code {
      private bool show = true;
}
`,
    "Components/CupPanel.razor": `<div style="border: 1px solid #ccc; padding: 8px; margin-top: 8px;">
      <button @onclick="() => cups++">Add cup</button>
      Cups: @cups
</div>

@code {
      private int cups; // component state
}
`,
  }}
/>

A page reload also clears component state, because the page and its components start fresh.

## Prerendering

### How It Works

- **Prerendering** means the server first sends the page as plain HTML, so the user sees it at once.
- Then the live connection starts, and the component renders **again**, now interactive.
- So `OnInitialized` and `OnInitializedAsync` run **twice**: once for the HTML, once when interactive.
- Prerendering is on by default for interactive components.

<figure className="blz-fig">
<svg viewBox="0 0 640 185" role="img" aria-labelledby="fig-rs-prerender">
  <title id="fig-rs-prerender">Animation in two steps. Step 1, prerender: the server sends plain HTML and the user sees the page, but the button is grey. Step 2, interactive: the SignalR line connects, OnInitialized runs a second time, and the button turns active.</title>
  <defs><marker id="fig-rs-prerender-a" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto"><path d="M0 0 L10 5 L0 10 z" style={{ fill: "var(--muted)" }} /></marker></defs>
  <rect className="box" x="20" y="20" width="280" height="150" rx="10" />
  <text className="t m" x="160" y="44" textAnchor="middle" style={{ fontSize: 12, fontWeight: 700 }}>1. Prerender (plain HTML)</text>
  <text className="t" x="160" y="74" textAnchor="middle" style={{ fontSize: 11 }}>OnInitialized runs (1st time)</text>
  <rect x="100" y="100" width="120" height="34" rx="6" style={{ fill: "#e5e7eb", stroke: "#9ca3af" }} />
  <text className="t" x="160" y="122" textAnchor="middle" style={{ fontSize: 12, fill: "#6b7280" }}>Add cup</text>
  <text className="t" x="160" y="156" textAnchor="middle" style={{ fontSize: 11 }}>visible, not clickable yet</text>
  <rect className="pur" x="340" y="20" width="280" height="150" rx="10"><animate attributeName="opacity" values="0.25;0.25;1;1" keyTimes="0;0.45;0.55;1" dur="5s" repeatCount="indefinite" /></rect>
  <text className="t m" x="480" y="44" textAnchor="middle" style={{ fontSize: 12, fontWeight: 700 }}>2. Interactive</text>
  <text className="t" x="480" y="74" textAnchor="middle" style={{ fontSize: 11 }}>OnInitialized runs (2nd time)</text>
  <rect className="grn" x="420" y="100" width="120" height="34" rx="6"><animate attributeName="opacity" values="0.25;0.25;1;1" keyTimes="0;0.45;0.55;1" dur="5s" repeatCount="indefinite" /></rect>
  <text className="t m" x="480" y="122" textAnchor="middle" style={{ fontSize: 12 }}>Add cup</text>
  <text className="t" x="480" y="156" textAnchor="middle" style={{ fontSize: 11 }}>clicks work</text>
  <path className="line" d="M300 95 H336" markerEnd="url(#fig-rs-prerender-a)" />
</svg>
<figcaption>Prerendering shows the page fast. The second render makes it interactive.</figcaption>
</figure>

- Until step 2, buttons show but do not react. You can disable them until the page is interactive with `RendererInfo.IsInteractive`.
- To switch prerendering off: `@rendermode @(new InteractiveServerRenderMode(prerender: false))`.

In the below example, the button is disabled while `RendererInfo.IsInteractive` is `false`. Code boxes are interactive at once, so here it is enabled straight away.

      Add cup
</button>

<p>Cups: @cups</p>
<p>@(RendererInfo.IsInteractive ? "Ready for clicks." : "Loading...")</p>

@code {
      private int cups;
}
`,
  }}
/>

### PersistentComponentState

- Because of the two renders, data is often **loaded twice**: once for the HTML and again when interactive. The page can also flicker.
- `PersistentComponentState` saves data from the first render **inside the HTML**. The second render reads it back instead of loading again.
- In .NET 10, the easy way is the `[PersistentState]` attribute on a **public** property. Then load only when the value is still empty: `Teas ??= await LoadTeas();`.

}
else
{
      @foreach (var tea in Teas)
      {
          <p>@tea</p>
      }
}

@code {
      [PersistentState]
      public List<string>? Teas { get; set; }

      protected override async Task OnInitializedAsync()
      {
          // Runs twice, but loads only once
          Teas ??= await LoadTeas();
      }

      private async Task<List<string>> LoadTeas()
      {
          await Task.Delay(500); // pretend to read the database
          return new() { "Masala Tea", "Ginger Tea" };
      }
}
`}
/>

- First render: `Teas` is empty, so the menu is loaded and saved into the page.
- Second render: `Teas` comes back from the page, so `??=` skips loading.
- Before .NET 10, you injected `PersistentComponentState` and called `RegisterOnPersisting` and `TryTakeFromJson` yourself. The attribute does that for you.

## Common mistakes

<div className="tscroll">

| Mistake | Wrong idea | Correct idea | What goes wrong |
|---|---|---|---|
| `@onclick` on a page with no `@rendermode` | Clicks always work | Add `@rendermode InteractiveServer` to the page | The button does nothing |
| `@rendermode InteractiveServer` without the `Program.cs` lines | The directive is enough | Add `AddInteractiveServerComponents()` and `AddInteractiveServerRenderMode()` | An error when the page opens |
| Loading data in `OnInitializedAsync` with prerendering | It runs once | It runs twice. Use `[PersistentState]` and `??=` | Data loads twice, and the page flickers |
| Expecting state after a page reload | The circuit keeps everything | A reload starts a new circuit | The values are lost |
| `[PersistentState]` on a private field | Any member works | Use it on a public property | The value is not saved |

</div>

## Check Yourself

## Exercises

## What to Remember

- A render mode says where a component runs and whether it is interactive.
- Static SSR: the server sends HTML once. No events. `[StreamRendering]` shows "Loading..." first. Enhanced navigation swaps only the changed parts.
- Interactive Server: C# runs on the server. A SignalR connection carries clicks and UI changes. Each tab has its own circuit.
- Set the mode with `@rendermode InteractiveServer` on a page, a component tag, or `<Routes />` for the whole app.
- Component state lives in a component's fields. It is lost when the component is removed or the page reloads.
- Prerendering renders twice. Use `RendererInfo.IsInteractive` to wait, and `[PersistentState]` to load data only once.

Next: **WebAssembly & Auto**
