Sub topic 7
Client references: how the payload points to client code
This chapter assumes you know React components, props and JSX, basic hooks such as useState, and promises with async/await. It also assumes you have a general idea of what a bundler does. Earlier chapters covered the "use client" boundary and the shape of the RSC payload. Here we look at one detail: how the payload refers to code that it never contains.
The problem a client reference solves
A Server Component runs on the server. When it renders a Client Component such as Counter, the server cannot run useState or attach a click handler. Those things only work in the browser.
The server also should not copy the source of Counter into the payload. The payload is a description of a tree, not a code bundle. So the server writes a placeholder that says, "a component lives here, and this is how to find it." That placeholder is a client reference.
A client reference is a small, serializable record. It names a module, the JavaScript chunks the browser must download to load that module, and the export to use from it.
What the server sees when it imports a Client Component
Look again at the two files from the first example.
// 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>
);
}// app/page.js (Server Component)
import Counter from './Counter';
export default function Page() {
return <Counter initial={3} />;
}In the server build, import Counter from './Counter' does not give page.js the real function. The bundler sees the "use client" directive and replaces the module with a stub. Each export of the stub is a client reference object. The server renderer recognizes these objects. When it meets one as an element type, it does not call it. It writes the reference into the payload and moves on.
This is why a Server Component can "render" a Client Component without running any of its code. The server only needs to know where the component can be found.
Reading a reference in the payload
Here is the simplified payload from the topic's example.
1:I["app/Counter.js",["chunk-counter.js"],"default"]
0:["$","main",null,{"children":[["$","h1",null,{"children":"Posts"}],["$","ul",null,{"children":[["$","li",null,{"children":"First post"}]]}],["$","$L1",null,{"initial":1}]]}]The exact syntax differs between React versions and frameworks. The idea stays the same. There are two kinds of row here.
The module reference row
Row 1 starts with I, which marks an import. It has three parts:
| Part | Example | Meaning |
|---|---|---|
| Module id | `"app/Counter.js"` | The key the client's module system uses to find the module |
| Chunks | `["chunk-counter.js"]` | The files the browser must load before the module is available |
| Export name | `"default"` | Which export of that module to use |
The element row that uses it
In row 0, the last child is ["$","$L1",null,{"initial":1}]. The "$" marks a React element. The type is "$L1", which means "a lazy reference to row 1." The props are {"initial":1}, plain JSON that the server computed.
So the element <Counter initial={1} /> appears in the payload as a type that points to row 1, plus its props.
Why the reference is lazy
The L in $L1 stands for lazy. The client may not have downloaded chunk-counter.js when it starts reading the payload. It cannot call or render Counter yet. It needs a placeholder that resolves later.
React gives the client a lazy-style wrapper around the module load. When the client parses row 1, it begins loading the listed chunks. When React renders the element, it asks the wrapper for the component. There are two cases:
- The chunk has arrived. The wrapper returns the real
Counterand rendering continues. - The chunk is still loading. The wrapper suspends, the same way
React.lazydoes, and the nearestSuspenseboundary shows its fallback.
This reuses the machinery that React.lazy and Suspense already provide. It also lets the payload arrive before the code does, and the client start loading code as soon as it sees the reference.
The diagram below shows the order of events.
The bundler's part
The payload only holds an id, chunk names and an export name. Those values are meaningful because the bundler produced them, using a manifest. A manifest is a lookup table that the build writes out.
The build does two jobs for each module marked "use client":
- In the server bundle, it replaces the module with reference stubs.
- In the client bundle, it keeps the real code and records which chunks contain it.
At render time, the server looks up each stub in the manifest to fill in the module id and chunks. In the browser, the module loader uses the same id to find the loaded code. If the two builds disagree, for example because the client and server bundles come from different deployments, the id may point to nothing. This is a common source of "module not found" errors after a deploy. Frameworks deal with it by building both sides together and versioning assets.
Which exports become references
A "use client" file can have several exports. Each export that a Server Component imports becomes its own reference, with its own export name.
// app/widgets.js
'use client';
import { useState } from 'react';
export function Toggle() {
const [on, setOn] = useState(false);
return <button onClick={() => setOn(!on)}>{on ? 'On' : 'Off'}</button>;
}
export function Badge({ label }) {
return <span className="badge">{label}</span>;
}// app/page.js (Server Component)
import { Toggle, Badge } from './widgets';
export default function Page() {
return (
<>
<Toggle />
<Badge label="new" />
</>
);
}The payload would contain two import rows for the same module id, one with export "Toggle" and one with "Badge". Both point to the same chunks, so the browser downloads the file once.
Notice that Badge has no state, yet it is still a Client Component. Everything exported from a "use client" module is client code. Its code is sent to the browser and it runs there, even when it does not need to. Splitting stateless pieces into a separate file without the directive keeps them on the server.
What a reference is not
A client reference is opaque to server code. This has practical effects.
- You cannot call it on the server. Calling an imported Client Component function as a plain function,
Counter({ initial: 1 }), throws an error, because the import is a stub and not the function. - You cannot read its fields. Accessing a property such as
Counter.someStaticValuefrom a Server Component does not return the real value. The server does not have the code. - You can pass it around. A reference is serializable, so a Server Component can pass
Counteras a prop to another Client Component, and the payload will carry the reference.
// app/Layout.js
'use client';
export default function Layout({ Sidebar }) {
return (
<div>
<Sidebar />
</div>
);
}// app/page.js (Server Component)
import Layout from './Layout';
import Counter from './Counter';
export default function Page() {
return <Layout Sidebar={Counter} />;
}Here Counter crosses the boundary as a reference, not as a function body. This works only for components defined in "use client" modules. Passing a normal function defined in a Server Component fails, which the next chapter on serializable props covers.
Common errors
- Using a hook in a Server Component. The file has no
"use client", so the server runs it directly anduseStateis not available. Move the stateful part into a file that starts with the directive. - Calling a Client Component as a function on the server. Render it as an element with JSX instead.
- Expecting a reference to carry runtime values. Only the module id, chunks and export name travel. Module-level variables in the client file exist only in the browser.
- Mismatched builds. A payload from one deployment pointing to chunks from another can fail to load. Keep server and client assets from the same build.
- Making too much a client module. Everything a
"use client"file imports also joins the client bundle. Keep the directive on small leaf files.
Summary
- A client reference is a serializable pointer to a client module: id, chunks and export name.
- The server build turns
"use client"modules into stubs, so the server never runs their code. - The payload stores references in import rows and refers to them from element rows through a lazy type.
- The browser loads the chunks, and React suspends on the reference until the code is ready.
- The bundler's manifest ties the server's ids to the client's chunks, so both builds must match.
Exercise
A Server Component imports Chart from a file that starts with 'use client' and renders <Chart data={[1, 2, 3]} />. Describe what appears in the payload for this element, and explain why the Chart code itself does not appear in it. What happens in the browser if the chunk for Chart has not finished loading when React renders the element?
Show answer
The payload has an import row for the Chart module holding the module id, the chunk files and the export name. It also has an element row whose type is a lazy reference to that import row, with the props {"data":[1,2,3]} as plain JSON. The Chart code is absent because the server build replaced the module with a stub, so the server only records where the code lives, and the client bundle holds the code. If the chunk is still loading, the lazy wrapper suspends and the nearest Suspense boundary shows its fallback until the chunk arrives. Then React renders Chart with the props.
Exercise
A file ui.js starts with 'use client' and exports Title (a stateless component) and Search (uses useState). Does Title run on the client or the server when a Server Component renders it? How could you keep Title on the server?
Show answer
Title runs on the client, because every export of a "use client" module is client code and is sent to the browser as a reference. To keep it on the server, move Title into a separate file without the directive and import it from there in the Server Component.