Sub topic 11
RSC is not SSR: how the two work together
This chapter assumes you know React components, props and JSX, useState and useEffect, and async/await. It also assumes you have seen the earlier chapters on Server and Client Components, the "use client" boundary and the RSC payload. You should know the difference between client-side rendering and server-side rendering in general terms.
Two ideas with similar names
People often treat "RSC" and "SSR" as the same thing, or as alternatives. They are different, and most RSC frameworks use both.
- Server-side rendering (SSR) turns a React tree into an HTML string on the server. The browser can show that HTML before any JavaScript has loaded. React then hydrates the HTML. Hydration means React attaches event handlers and state to the existing DOM so the page becomes interactive.
- React Server Components (RSC) decide where each component runs and what the server sends to the client. The server runs Server Components and sends their output as the RSC payload. That payload is a serialized description of a React tree, not HTML.
The two answer different questions:
| Question | SSR | RSC |
|---|---|---|
| What does the server produce? | An HTML document | An RSC payload (serialized element tree plus references) |
| Who consumes the output? | The browser's HTML parser, then React for hydration | React running on the client or in an SSR process |
| Does component code run on the server? | Every component runs once on the server | Only Server Components run there, and only on the server |
| Does component code reach the browser? | All components, so they can hydrate | Only Client Components |
| Main goal | Fast first paint and crawlable HTML | Less client JavaScript, direct server data access, a server-aware tree |
Because the questions differ, you can have either one without the other. A classic SSR app has no RSC: every component is shipped to the browser and hydrated. An RSC app could in principle send only payloads and let the client render them, with no HTML step. Frameworks usually combine them.
What SSR does with Client Components
A common misconception is that "Client Component" means "renders only in the browser". In an SSR setup, Client Components also render on the server, once, to produce the initial HTML. The "use client" directive controls where the code is bundled and hydrated. It does not stop the module from running on the server during SSR.
Take the Counter from earlier:
// app/Counter.js
'use client';
import { useState } from 'react';
export default function Counter({ initial }) {
const [count, setCount] = useState(initial);
return (
<button onClick={() => setCount(count + 1)}>
Count: {count}
</button>
);
}During SSR, React renders this component on the server with initial set to its prop value. useState returns the initial value, and the output is a <button> with the text Count: 1. Effects such as useEffect do not run during SSR. Event handlers like onClick are not attached to the HTML. Only hydration in the browser attaches them.
So the code you wrote for a Client Component must be safe to run in both environments for its first render. Reading window or localStorage in the render body will fail during SSR. Put such reads in useEffect, which runs only in the browser.
The two-stage pipeline
In a typical framework, a request passes through two renderers, one after the other.
- RSC render. The server runs the Server Components starting from the root. It produces an RSC payload. Client Components appear in the payload as client references, not as their rendered output.
- SSR render. A second renderer consumes the payload as a stream. It rebuilds the React tree. For each client reference it loads the Client Component module and renders it to HTML. The result is an HTML stream.
- Delivery. The server sends the HTML to the browser. The same payload is usually embedded in the page as well, so the client does not need a second request.
- Hydration. In the browser, React reads the embedded payload, rebuilds the same tree, loads the Client Component chunks, and hydrates the HTML.
The key point is that the HTML is derived from the payload. The payload is the primary description of the UI, and HTML is one way to present it before JavaScript loads.
Why the payload is still needed after SSR
If the server already sent HTML, why send the payload as well? Because HTML cannot carry what React needs.
- HTML loses the component structure. Hydration needs to know which parts of the DOM belong to which Client Component and what props each received.
- HTML cannot represent references to client modules or pending Suspense content.
- On later navigations, the client needs a tree it can merge with the existing one. HTML would replace DOM nodes and discard client state. The next chapter's topic, applying payloads on the client, depends on this.
So the payload serves hydration on the first load and tree updates on later loads. The HTML only improves what the user sees first.
Server Components never hydrate
Server Components run only on the server. Their code is not in the browser bundle. This has a consequence for hydration: React does not need to hydrate them, because they have no state or handlers. Their output appears in the HTML as static markup and in the payload as plain elements.
Compare two pages that render the same list of posts:
- In a classic SSR app, the component that renders the list is bundled and hydrated. The browser downloads its code and the code of any library it uses, such as a Markdown parser.
- With RSC, if the list component is a Server Component, the Markdown parser stays on the server. The browser receives HTML for the first paint and the payload for the tree. Only the interactive
Counteris hydrated.
This is where much of the bundle-size saving in RSC comes from. SSR alone does not give you that saving, because SSR still ships the code for hydration.
What the user sees in each case
A timeline helps compare the cases.
| Setup | First visible content | Interactive when |
|---|---|---|
| Client-side rendering only | After the JavaScript bundle loads and runs | After the same step |
| SSR without RSC | HTML arrives, shown immediately | After the full bundle loads and hydrates |
| RSC with SSR | HTML arrives, shown immediately | After only the Client Component chunks load and hydrate |
| RSC without SSR | After the client loads React and the payload | After the Client Component chunks load |
RSC with SSR gives both a fast first paint and less JavaScript to hydrate. RSC without SSR still reduces bundle size, but the user waits for JavaScript before seeing anything from the app shell.
Streaming works in both layers
Suspense streaming, covered in the previous chapter, applies to both renderers. When a Server Component is wrapped in <Suspense>, the RSC render sends the surrounding payload first and the suspended part later. The SSR renderer also streams: it emits HTML with the fallback in place, then later emits the real HTML and a small script that swaps it in.
import { Suspense } from 'react';
import Reviews from './Reviews'; // async Server Component
export default function Product() {
return (
<section>
<h1>Product</h1>
<Suspense fallback={<p>Loading reviews...</p>}>
<Reviews />
</Suspense>
</section>
);
}The browser first receives HTML containing the heading and the "Loading reviews..." fallback. When Reviews finishes, the server sends more HTML for it and the matching payload rows. Hydration can proceed for already-arrived parts while the rest streams in.
Common errors
- Assuming
"use client"means browser-only. The module also runs on the server during SSR. Code that toucheswindowduring render will throw there. - Hydration mismatch. If a Client Component renders different output on the server and in the browser, for example by using
Date.now()orMath.random()in render, React warns that the HTML does not match. Compute such values in an effect, or pass them as props from the server. - Expecting Server Component code in the browser. If you use
console.login a Server Component, the output appears in the server logs. The browser does not run it. - Thinking RSC removes the need for SSR. Without SSR, users see nothing from the app until JavaScript loads. Most frameworks keep SSR for this reason.
- Confusing the payload with HTML. Inspecting the network tab, you will see the payload as a text stream of rows. It is not a page you can render in a browser.
Decision criteria
When deciding how to structure a page, these rules of thumb follow from the model:
- Put data fetching and heavy, non-interactive rendering in Server Components. They add no client JavaScript and need no hydration.
- Put state, effects and event handlers in Client Components, as small and as deep in the tree as practical.
- Keep SSR on for pages where first paint and search-engine visibility matter. Consider turning it off only for views that cannot render meaningfully without browser-only APIs.
- Write Client Components so their first render is identical on the server and in the browser.
Exercise
A Client Component reads window.innerWidth directly in its render body and the app uses RSC with SSR. What goes wrong, and how do you fix it?
Show answer
The component also renders on the server during SSR, where window does not exist, so the render throws. Move the read into useEffect (and store it in state), because effects run only in the browser. Alternatively, guard the access, but the effect approach also avoids a hydration mismatch between server HTML and the first browser render.
Exercise
Why does the server often send the RSC payload to the browser even though it has already sent HTML?
Show answer
HTML does not carry the component structure, the props passed to Client Components, or the client references. Hydration needs the payload to rebuild the same tree and attach it to the existing DOM. Later navigations also use payloads to update the tree without losing client state.
Exercise
In an RSC app with SSR, which components are hydrated in the browser, and which are not?
Show answer
Only Client Components are hydrated. Server Components are rendered on the server, appear as static markup in the HTML and as elements in the payload, and their code is not in the browser bundle, so there is nothing to hydrate.