Sub topic 11
RSC is not SSR: how the two work together
This chapter assumes you know React components, props and hooks, and have read the earlier chapters on Server Components, the "use client" boundary and the RSC payload. Here you will separate two ideas that people often merge: React Server Components (RSC) and server-side rendering (SSR).
Two different jobs
RSC and SSR answer different questions.
- RSC decides where a component runs. A Server Component runs only on the server. Its output is sent as an RSC payload, a serialized description of the React tree.
- SSR decides what the first response looks like. React renders components to an HTML string, so the browser can show content before any JavaScript loads.
You can use either one without the other. A client-only app uses neither. An older SSR app renders every component to HTML on the server, then runs all of those components again in the browser to attach event handlers. That process is called hydration. Nothing in that setup is a Server Component.
| Question | RSC | SSR |
|---|---|---|
| What does it produce? | The RSC payload (a serialized tree) | An HTML document |
| Who consumes the output? | React on the client | The browser's HTML parser |
| Does component code ship to the browser? | Not for Server Components | Yes, for every component that must hydrate |
| Does it need hydration? | The payload itself is not hydrated | The HTML is hydrated |
How they combine
A framework that supports both runs two passes on the server for the first page load.
- RSC pass. React renders the Server Components and produces the RSC payload. Client Components appear in it as client references, not as output.
- SSR pass. React takes that payload and renders it, including the Client Components, into HTML.
The server sends the HTML first, followed by the payload. The browser shows the HTML immediately. Then the client JavaScript loads, React reads the payload and hydrates only the Client Components. Server Components are never hydrated, because their code is not in the browser.
On later navigations, the framework usually fetches only a new payload. It does not request a full HTML document. The earlier chapter on navigation covers that step.
Client Components still render on the server
A common mistake is to read "use client" as "this runs only in the browser." During the SSR pass, a Client Component renders once to HTML on the server. Its useState initial value is used. Its useEffect does not run, because effects run only in the browser.
'use client';
import { useState, useEffect } from 'react';
export default function Clock() {
const [now, setNow] = useState(null);
useEffect(() => {
setNow(new Date().toLocaleTimeString());
}, []);
return <p>{now ?? 'Loading time...'}</p>;
}The server HTML contains Loading time.... After hydration, the effect runs and the time appears. If you read window or call new Date() directly during render, the server and browser can produce different output. That causes a hydration mismatch warning.
Why use both
- RSC keeps server-only code and its dependencies out of the browser bundle.
- SSR gives users visible content before the JavaScript arrives.
Without SSR, an RSC app still works, but the user waits for JavaScript and the payload before seeing anything. Without RSC, SSR gives a fast first paint but ships every component to the browser.
Exercise
A page has one Server Component that fetches posts and one Client Component with a button. On the first load of an app with RSC and SSR, which components are hydrated in the browser, and why?
Show answer
Only the Client Component is hydrated. The Server Component's output arrives in the payload as plain tree data and its code is not in the browser, so there is nothing to hydrate. The Client Component's code is shipped, so React attaches its event handlers to the server-rendered HTML.
Exercise
A Client Component reads window.innerWidth directly in its render body. It works in the browser but the SSR pass throws an error. Why, and how do you fix it?
Show answer
Client Components also render on the server during SSR, where window does not exist. Move the read into a useEffect (storing the value in state), or into an event handler, so it runs only in the browser.