Server Rendering

By Syncfusion®, Inc. · Last updated

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.

What you will learn

  • Tell the four Blazor render modes apart
  • Explain how Static SSR, streaming and enhanced navigation work
  • Use Interactive Server with @rendermode and explain circuits
  • Explain component state, prerendering and [PersistentState]

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:
Render modeWhere the C# runsInteractive?
Static SSROn the server, once per page requestNo
Interactive ServerOn the server, over a live connectionYes
Interactive WebAssemblyIn the browserYes
Interactive AutoOn the server first, then in the browserYes
  • 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.

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.

Static SSR 🖥️ server, no clicks

Interactive Server 🖥️⇄🌐 server, live link

Interactive WebAssembly 🌐 in the browser

Interactive Auto 🖥️ → 🌐 server first, then browser This lesson Next lesson

The four render modes. This lesson covers the two server modes.

  • 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.

  • 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.

Click Next to see each command.

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.

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.

Browser Menu: Masala ₹20

Server runs Menu.razor makes HTML

GET /menu HTML

Static SSR: one request, one HTML answer, then the connection closes.

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.

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".

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.

Normal link

Whole page reloads Enhanced navigation

☕ Ravi's Tea Shop (stays)

Menu page Order page

Enhanced navigation keeps the page and changes only what is different.

Use Cases and Limitations

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

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.

Click Next to see each command.

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.

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.

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.

Asha's tab

Ravi's tab

Server

Circuit 1: cups = 2

Circuit 2: cups = 5

SignalR SignalR

Every tab has a live SignalR line and its own circuit on the server.

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.

  • 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.

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.

Browser

Rejoining the server...

Server circuit kept

SignalR

A lost connection shows the reconnect box until the line is back.

Use Cases and Limitations

Good forLimits
Fast first start: nothing big to downloadNeeds a live connection all the time
Code and secrets stay on the serverEvery click waits for a trip to the server
Easy access to the database and server filesEach user's circuit uses server memory
Office and business apps on a good networkDoes not work offline

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.

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.

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.

1. Prerender (plain HTML) OnInitialized runs (1st time)

Add cup visible, not clickable yet

2. Interactive OnInitialized runs (2nd time)

Add cup clicks work

Prerendering shows the page fast. The second render makes it interactive.

  • 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.

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();.
  • 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

MistakeWrong ideaCorrect ideaWhat goes wrong
@onclick on a page with no @rendermodeClicks always workAdd @rendermode InteractiveServer to the pageThe button does nothing
@rendermode InteractiveServer without the Program.cs linesThe directive is enoughAdd AddInteractiveServerComponents() and AddInteractiveServerRenderMode()An error when the page opens
Loading data in OnInitializedAsync with prerenderingIt runs onceIt runs twice. Use [PersistentState] and ??=Data loads twice, and the page flickers
Expecting state after a page reloadThe circuit keeps everythingA reload starts a new circuitThe values are lost
[PersistentState] on a private fieldAny member worksUse it on a public propertyThe value is not saved

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