Sub topic 17
Where Server Components fit relative to SSR
This chapter assumes you have read the earlier chapters on renderToPipeableStream, hydrateRoot, Suspense boundaries, and hydration mismatches. It also assumes you can read a basic React component with useState. It does not teach you to build a Server Components setup. It explains how Server Components relate to the SSR pipeline you have built, so you can place them correctly in your mental model.
Two features with similar names
Server-side rendering (SSR) and React Server Components (RSC) are separate features. They solve different problems, and people often confuse them.
- SSR takes ordinary components, runs them once on the server to produce HTML, and sends that HTML so the user sees content before JavaScript loads. The same components then run again in the browser during hydration. Every component's code is still shipped to the client.
- Server Components are components that run only on the server. Their code is not sent to the browser, and they are never hydrated. They can read data directly (a database, the file system, a private API) and return a description of UI.
SSR answers the question "when does the user first see HTML?" Server Components answer the question "which components need to exist in the browser at all?"
| Question | SSR | Server Components |
|---|---|---|
| Where does the component run? | Server, then again in the browser | Server only |
| Is its code in the client bundle? | Yes | No |
| Does it hydrate? | Yes | No |
| Can it use `useState` or `useEffect`? | Yes | No |
| Can it read a database directly? | Not safely | Yes |
| What does the server produce? | HTML | A serialized tree called the RSC payload |
What the server actually produces
SSR output is HTML text. The browser parses it and shows it. React then needs the component code and the same props to hydrate it.
A Server Component does not produce HTML directly. React renders it into the RSC payload. This is a compact, streamable description of the tree: the elements that were rendered, plus references to Client Components that the browser must load. The payload is not a page. It is data that React on the client can merge into its tree.
In a full framework setup, the two features run in sequence:
- The server renders Server Components into the RSC payload.
- An SSR step turns that payload, together with the Client Components it references, into HTML.
- The browser shows the HTML immediately.
- The browser receives the payload and the Client Component code, then hydrates only the Client Components.
Notice that the HTML in step 3 is the same kind of output you produced with renderToPipeableStream earlier. The SSR step still exists. Server Components add a stage before it, not a replacement for it.
Client Components are still the SSR components you know
In an RSC-enabled framework, a file marked with the 'use client' directive defines a Client Component. The name is misleading. Client Components are still rendered to HTML on the server during SSR, and they are then hydrated in the browser. They behave exactly like the components in the rest of this tutorial.
Here is a Server Component and a Client Component working together. The Server Component is async and fetches data during render. This is a pattern only Server Components allow.
// app/page.js
// In a framework with RSC support, files are Server Components by default.
import LikeButton from './LikeButton.js';
export default async function Page() {
const res = await fetch('https://api.example.com/posts/1');
const post = await res.json();
return (
<article>
<h1>{post.title}</h1>
<p>{post.body}</p>
<LikeButton initialLikes={post.likes} />
</article>
);
}// app/LikeButton.js
'use client';
import { useState } from 'react';
export default function LikeButton({ initialLikes }) {
const [likes, setLikes] = useState(initialLikes);
return (
<button onClick={() => setLikes(likes + 1)}>
{likes} likes
</button>
);
}Page never reaches the browser. Only LikeButton, react, and the payload do. The <h1> and <p> arrive as HTML and as payload data, but React does not hydrate them because nothing in the browser needs to re-run them.
What this means for hydration
Server Components change what hydration has to do, and where mismatches can occur.
- Less to hydrate. Hydration only attaches to Client Components. A page that is mostly static content has less JavaScript to download and less work to do on load.
- Server Components cannot mismatch. A mismatch happens when the client's first render differs from the server HTML. A Server Component has no client render, so there is nothing to compare. Reading
new Date()in a Server Component is not a hydration bug, because the value is computed once and sent as is. - Client Components can still mismatch. Everything from earlier chapters still applies inside them: time and randomness during render,
windowchecks, invalid nesting, and ids. The fixes are the same: effects,useId, anduseSyncExternalStorewith a server snapshot. - Props are a new boundary. Props passed from a Server Component to a Client Component must be serializable, because they travel in the payload. Plain objects, arrays, strings, numbers, and dates are fine. Functions and class instances are not.
// app/page.js (Server Component)
import Widget from './Widget.js';
export default function Page() {
// Error: a function cannot be serialized into the RSC payload.
return <Widget onSelect={() => console.log('picked')} />;
}If a Client Component needs behavior, define the handler inside the Client Component. If it needs data, pass the data as props.
Streaming and Suspense still apply
Server Components build on the streaming model from earlier chapters. An async Server Component suspends while it awaits data, so wrapping it in a Suspense boundary lets the server send the surrounding page first and stream the slow part when it resolves. Selective hydration then hydrates the Client Components inside each boundary independently, as you saw before.
import { Suspense } from 'react';
import Comments from './Comments.js'; // an async Server Component
export default function Page() {
return (
<main>
<h1>Article</h1>
<Suspense fallback={<p>Loading comments...</p>}>
<Comments />
</Suspense>
</main>
);
}Compare this with the lazy example from the Suspense chapter. There, Comments was a Client Component loaded on demand, and its code still shipped to the browser. Here, the component's code stays on the server.
You do not need Server Components for SSR
The setup you built with renderToPipeableStream and hydrateRoot is complete SSR without Server Components. It is a valid choice when you control your own server and want to keep the architecture simple.
Server Components are not something you enable with a flag in that setup. They need a bundler and server integration that can split server and client modules, produce the payload, and wire the two together. In practice you get this from a framework that supports it. Treat Server Components as a framework feature that builds on the SSR and streaming primitives, not as a replacement for them.
Choosing between the approaches
- Client rendering only: simplest, but the user sees a blank page until JavaScript runs.
- SSR with streaming (this tutorial): fast first paint, with full control of the server. All component code still ships and hydrates.
- Server Components with SSR: fast first paint and less client JavaScript. This requires a supporting framework and a strict split between server-only and interactive code.
Exercise
A page uses a framework with Server Components. A component named Footer reads new Date().getFullYear() during render and has no 'use client' directive. Another component, Greeting, has 'use client' and renders new Date().toLocaleTimeString(). Which one can cause a hydration mismatch, and why?
Show answer
Greeting can. It is a Client Component, so it renders on the server for SSR and again in the browser during hydration, and the two time values will likely differ. Footer is a Server Component. It runs only on the server, its output is sent once, and it is never hydrated, so there is no client render to disagree with. Fix Greeting by rendering a placeholder first and setting the time in useEffect.
Exercise
A Server Component passes onClick={() => save()} as a prop to a Client Component, and the build fails. Why does it fail, and what is the fix?
Show answer
Props from a Server Component to a Client Component are serialized into the RSC payload, and functions cannot be serialized. Define the handler inside the Client Component, or pass only serializable data and let the Client Component decide what to do with it.