Sub topic 6
What the bundler and framework do
This chapter assumes you know React components, props, ES modules, and what a bundler does. React Server Components (RSC) need more than React itself. React defines the payload format and the rules for rendering. A bundler and a framework supply the parts that React cannot supply on its own.
Why React alone is not enough
React can render a tree and serialize it. It cannot decide which files are client code, produce the JavaScript files a browser will download, or route a request to a component. Those jobs belong to the bundler and the framework.
| Job | Who does it |
|---|---|
| Define the payload format and render rules | React |
| Find `"use client"` and `"use server"` directives in modules | Bundler plugin |
| Build separate server and client outputs | Bundler |
| Map a module to its browser chunk files | Bundler manifest |
| Map URLs to pages, handle requests, send responses | Framework |
What the bundler does
The bundler reads your module graph and treats the "use client" directive as a split point. It runs at least two builds from one source tree.
- Server build. Server Components stay as normal code. When a server module imports a file marked
"use client", the bundler does not include that file's code. It replaces the import with a client reference, a small stand-in that records the module's identity. - Client build. The bundler starts from every module marked
"use client", follows their imports, and emits browser chunks. - Manifest. The bundler writes a map from each client module to the chunk files that contain it and the export name to use.
Look again at the payload from the earlier chapter:
1:I["app/Counter.js",["chunk-counter.js"],"default"]The module path, the chunk list, and the export name default all come from the manifest. The server does not know where Counter lives in the browser. It asks the manifest and writes the answer into the row.
What the framework does
The framework wires the pieces into a working application. Typical responsibilities:
- Routing. It maps a URL such as
/poststo a root Server Component likeapp/page.js. - Running the render. It calls React's server renderer with the route's tree, the manifest, and any request data (headers, cookies, params).
- Responding. It returns the payload as a stream, and often also produces HTML from it (covered in a later chapter).
- Client runtime. It ships the code that fetches payloads, loads the chunks named in client references, and applies results during navigation.
- Server Function endpoints. It exposes a network endpoint for each
"use server"function and turns calls into requests.
Gotchas
- The directive is a build-time signal. The bundler reads
"use client"statically. Writing it after an import, or computing it, does not work. Put it at the top of the file, before any imports. - Everything a client module imports is client code. The boundary covers the whole import subtree, so a large library imported by a client file lands in the browser bundle.
- Plain bundler setup is not enough. Running a stock build without RSC support will fail or treat every file as client code. Use a framework or a bundler plugin built for RSC.
- Payload details vary. Row syntax and manifest shape differ between React versions and frameworks. Do not depend on them in application code.
Exercise
A server module imports Counter.js, which starts with 'use client'. What does the server build contain for that import, and where does the browser find the real code?
Show answer
The server build contains a client reference, not the code of Counter.js. The payload row points to the module and its chunk files, which the bundler's manifest supplies. The browser downloads the chunk from the client build and loads the named export.