Sub topic 6
What the bundler and framework do
This chapter assumes you know React components, props and hooks, ES modules, and what a bundler is. You also need the earlier idea that a "use client" module marks where client code begins. Here you will see why React alone cannot make Server Components work, and which jobs the bundler and the framework take on.
Why React needs a bundler and a framework
React defines the component model and the Flight format that carries the RSC payload. React does not know how your files are laid out. It does not know how to turn an import into a downloadable file, how to answer an HTTP request, or how to map a URL to a component.
Those jobs belong to two layers.
- The bundler (Webpack, Turbopack, Vite and similar tools) reads your module graph, splits it by environment, and produces files for each.
- The framework (Next.js, React Router in framework mode, Waku and similar tools) runs the server, routes requests, calls React's server renderer, and applies payloads in the browser.
React exposes low-level APIs for both layers to plug into. You rarely call them yourself, but knowing they exist explains many behaviors that look like magic.
One source tree, two module graphs
A project that uses Server Components is compiled at least twice. Each pass sees a different slice of your code.
- The server graph starts at your server entry and includes Server Components, data access code, and server-only libraries.
- The client graph starts at every module marked
"use client"and includes everything those modules import.
The same file can appear in both graphs. A shared utility imported by a Server Component and by a Client Component is compiled for each environment. Server Component code never appears in the client graph, so it is never downloaded.
What happens at a "use client" import
Consider the Counter module from the example. It starts with 'use client', and app/page.js imports it.
// 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>
);
}When the bundler builds the server graph and meets import Counter from './Counter', it does not include the real function body. A bundler plugin replaces the module with a small stub. Calling or rendering that stub as a normal component is an error. Instead, the stub is a client reference: an object that records two facts.
- Which module the code lives in, such as
app/Counter.js. - Which export is wanted, such as
default.
When React renders the Server Component tree and reaches <Counter />, it sees a client reference instead of a function to call. It does not run the component. It writes a pointer into the payload and keeps going.
On the client side, the same plugin makes Counter.js a normal module. It goes into a chunk that the browser can download.
The client manifest
The pointer in the payload has to resolve to something the browser can load. The bundler produces a client manifest for this. It maps a module ID and export name to the chunk files that contain the code.
Here is the line from the simplified payload shown earlier.
1:I["app/Counter.js",["chunk-counter.js"],"default"]Read it as three parts: the module ID (app/Counter.js), the chunks to load (chunk-counter.js), and the export name (default). The exact row syntax varies between React versions and frameworks. The three facts are always present.
The manifest has two uses.
- On the server, it lets React write the row above when it meets a client reference.
- On the client, it lets the framework's loader fetch the chunk and pick the right export before React renders the element.
If the manifest is wrong or stale, the symptom is usually a payload that references a chunk that does not exist, or a client that cannot find an export. Deploying the server and client from different builds causes this. Keep them in sync.
The server side of the framework
At request time the framework does the following.
- Matches the URL to a route, such as
app/page.js. - Builds the root element, usually the page wrapped in its layouts.
- Hands that element to React's server renderer, together with the client manifest.
- Receives a stream of payload chunks back.
- Sends the stream to the browser, or feeds it to an HTML renderer (covered in a later chapter).
Here is what a Server Component actually looks like to this machinery. Nothing in it is framework-specific except the file location and the async function.
// app/page.js (Server Component by default in an RSC framework)
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>
);
}React awaits Page, walks the returned elements, and emits rows. The fetch call runs in the server environment, so credentials and internal URLs stay there.
The client side of the framework
In the browser, the framework supplies a bootstrap script that does these things.
- Reads the payload stream, from an embedded script or a network response.
- Passes the stream to React's client-side reader, which decodes rows into elements.
- When the reader meets a client reference, asks the framework's module loader to fetch the chunk and return the export.
- Renders the decoded tree into the DOM and later swaps in new trees during navigation.
React's reader only knows how to ask for a module. How the chunk is fetched, cached, and executed is a bundler detail. That is why Webpack, Turbopack and Vite each need their own plugin or integration.
Division of labor
| Job | React | Bundler | Framework |
|---|---|---|---|
| Define the Flight format | Yes | No | No |
| Split server and client module graphs | No | Yes | Configures it |
| Replace `"use client"` imports with client references | No | Yes (plugin) | Installs the plugin |
| Produce the client manifest | No | Yes | Passes it to React |
| Route a URL to a component | No | No | Yes |
| Serve the payload over HTTP | No | No | Yes |
| Load a client chunk on demand | Asks | Provides the loader | Wires them together |
| Decode rows into elements | Yes | No | Calls it |
Common errors and what they mean
- "You're importing a component that needs
useState. It only works in a Client Component." The module that uses a hook was reached from the server graph without a"use client"directive. Add the directive to the module that needs client features, or move the hook into a smaller Client Component. - A server-only library ends up in the client bundle. A
"use client"module imported it, directly or through another module. Anything reachable from a client module is in the client graph. Move that import to the server side, and pass data down as props. - Hydration or load errors after a deploy. The browser holds an old manifest or old chunks while the server sends a newer payload. Version your assets, and have the framework reload the page when it detects a mismatch.
- A client reference called like a function. Code such as
Counter()inside a Server Component fails, because the server side holds only a reference. Render it as<Counter />instead.
Why this matters for your decisions
Knowing the build split changes how you design components.
- Bundle size follows the boundary. Moving
"use client"lower in the tree keeps more code in the server graph and out of the download. - Server-only code is safe only if it stays in the server graph. Tools such as a
server-onlyimport guard make the bundler fail the build if a client module reaches it. - Framework choice affects behavior you may assume is part of React, such as routing, caching and how chunks are prefetched.
Exercise
A Server Component imports a Client Component from app/Chart.js, which begins with 'use client'. In the server build, what does the import resolve to, and what does the payload contain for that element?
Show answer
In the server build, the bundler plugin replaces the import with a client reference that records the module ID (app/Chart.js) and export name. React does not run the Chart function on the server. The payload contains a module row built from the client manifest (module ID, chunk files, export name) and an element row that points to that row, plus the serialized props. The browser loads the chunk and renders Chart there.
Exercise
After a deployment, some users see errors about missing chunks when they navigate. Which part of this chapter's model explains the cause, and what is a sensible fix?
Show answer
The client manifest maps module IDs to chunk files, and the payload from a newer server build points to chunks the browser's older build does not have, or the reverse. The fix is to keep server and client output from the same build, version or hash the assets, keep old chunks available for a while, and have the framework do a full page reload when it detects a mismatch.