Sub topic 4
How the server renders a tree for the client
This chapter assumes you know React components, props and JSX, async/await, and the difference between a Server Component and a Client Component. It covers the steps the server takes to turn a component tree into something the browser can use.
The renderer walks the tree
A Server Component is a function that returns React elements. Elements are plain objects that describe what to show. The server renderer starts at the root element and works down:
- If the element is a host element such as
mainorli, the renderer keeps it and renders its children. - If the element is a Server Component, the renderer calls the function, waits for the result if the function is
async, and then renders what it returned. - If the element is a Client Component, the renderer does not call it. It records a reference to the component's module and keeps the props.
The result is a tree that contains only host elements, text, and client references. No Server Component functions remain in it.
// app/page.js
import Counter from './Counter';
async function getPosts() {
const res = await fetch('https://example.com/api/posts');
return res.json();
}
export default async function Page() {
const posts = await getPosts();
return (
<main>
<h1>Posts</h1>
<ul>
{posts.map((p) => (
<li key={p.id}>{p.title}</li>
))}
</ul>
<Counter initial={posts.length} />
</main>
);
}For this page, the renderer calls Page and waits for fetch. The main, h1, ul and li elements come out as resolved output. Counter stays unresolved, with the prop initial set to the number of posts.
Where the work happens
The framework handles routing and decides which component is the root for a request. React handles the walk through the tree. The data source is any await, such as a database, a file or a fetch call.
Rendering happens once per request
Server Components run on the server and do not run again in the browser. This has consequences:
- They cannot use
useState,useEffector event handlers, because nothing re-renders them on the client. - They can read secrets, query a database directly, and import large libraries without adding to the browser bundle.
- Their result is output, not code. The browser receives what the component produced, not the component itself.
Serialization
The renderer does not hold the finished tree in memory and send it later. It converts the tree into a serialized stream as it goes. Each resolved piece is written out, and any piece that is still waiting on data can be sent after the rest. The next chapter shows what this stream looks like. For now, note that the output is a description of a React tree, not HTML.
Common errors
- Expecting a Server Component to re-run on a click. It does not. State changes belong in a Client Component, or in a new request that renders the tree again.
- Passing a function as a prop to a Client Component. The prop must be serializable, and a plain function is not. Serialization rules get their own chapter.
- Assuming a Client Component never runs on the server. Client Components are skipped by the Server Component pass, but a framework may still render them to HTML separately.
Exercise
A Server Component renders <main><Counter initial={3} /></main>, where Counter has 'use client' at the top of its module. What does the server renderer do when it reaches Counter, and which values does it keep?
Show answer
It does not call Counter. It records a reference to the Counter module and keeps the props, here initial with the value 3. The main element is resolved as ordinary output.