Sub topic 17
Where Server Components Fit Relative to SSR
This chapter assumes you know how renderToPipeableStream, Suspense boundaries, and hydrateRoot work from the earlier chapters. It places React Server Components (RSC) next to SSR so you don't confuse the two. It is an orientation, not a full RSC guide.
Two different questions
SSR and Server Components answer different questions.
- SSR asks: where does the first HTML come from? Components render to HTML on the server, then the browser hydrates them.
- Server Components ask: where does a component's code run, and does that code ever ship to the browser?
A Server Component runs only on the server. Its code is not sent to the client, and it is never hydrated. It can read from a database or the file system directly. A Client Component is the kind of component you have written so far. It can use state, effects, and event handlers, and it is hydrated in the browser.
The names are misleading. Client Components are still rendered to HTML on the server during SSR. "Client" means the component's code is also included in the browser bundle.
How they combine
An RSC framework uses both. The server first renders Server Components into a serialized description of the tree, called the RSC payload. SSR then turns that tree into HTML for the first paint. In the browser, React hydrates only the Client Components and reuses the payload for the rest.
What changes for hydration
Selective hydration still applies. Suspense boundaries remain the units that stream and hydrate independently. The difference is how much code there is to hydrate.
| Concern | SSR only | SSR with Server Components |
|---|---|---|
| Code shipped to the browser | Every component | Client Components only |
| Components hydrated | Every component | Client Components only |
| Data access | Fetch in an API or loader | Directly inside a Server Component |
| State and effects | Anywhere | Client Components only |
Server Components also reduce one source of mismatches. A Server Component is never re-rendered in the browser, so it cannot disagree with the server HTML. Mismatches still occur in Client Components, and the fixes from earlier chapters (effects, useId, useSyncExternalStore) still apply there.
Marking a Client Component
In frameworks that support RSC, a file is a Server Component by default. You opt a file into the client with a directive at the top.
'use client';
import { useState } from 'react';
export default function LikeButton() {
const [liked, setLiked] = useState(false);
return (
<button onClick={() => setLiked(!liked)}>
{liked ? 'Liked' : 'Like'}
</button>
);
}A Server Component can import and render this button. The reverse does not work. A Client Component cannot import a Server Component, although it can receive one as children.
Exercise
Exercise
A page uses SSR with Server Components. A component fetches product data from a database and has no state or event handlers. Another component holds a quantity counter with useState. Which one is hydrated in the browser, and why?
Show answer
The quantity counter is hydrated. It is a Client Component because it uses state and needs event handlers in the browser. The product component is a Server Component. It runs only on the server and its code is not sent to the browser, so there is nothing to hydrate. Both components still appear in the first HTML.