Sub topic 7
Client references: how the payload points to client code
This chapter assumes you know React components, props and hooks, async/await, and the basics of ES modules and bundlers. It also assumes you have seen the "use client" directive. Here you will see how the RSC payload refers to client code without including that code.
The problem: the server cannot ship a function
A Server Component renders to a tree of elements. Some of those elements are Client Components such as Counter. A Client Component is a function, and the payload is a text stream. Functions cannot be serialized. The server also should not run Counter, because it uses state and event handlers that only make sense in the browser.
The payload therefore carries a client reference. This is a small description of where the component lives. It says which module and which export to load. It does not contain the code.
From import to reference
In a Server Component file, this import looks normal:
import Counter from './Counter';Counter.js starts with 'use client'. When the bundler builds the server side, it sees that directive. It does not include the real component body. It replaces the module's exports with stub objects. Each stub records three things: the module id, the chunk files to load, and the export name.
When the server renders <Counter initial={1} />, it finds a stub where it expected a function. It does not call anything. It writes a reference into the payload and keeps the props.
What it looks like in the payload
This is the simplified payload from the earlier chapter. Exact syntax varies between React versions and frameworks.
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}]]}]Row 1 starts with I, which marks a module import. It holds the module path, the list of chunks to fetch, and the export name ("default"). Row 0 is the element tree. Inside it, "$L1" is the element type. The $L prefix means a lazy reference to row 1. Lazy means the client resolves it later, after the chunk has loaded.
| Part | Meaning |
|---|---|
| `1:I[...]` | Row 1 is a client module reference |
| `"app/Counter.js"` | Module id known to the bundler |
| `["chunk-counter.js"]` | Files the browser must fetch |
| `"default"` | Which export to use |
| `"$L1"` | Lazy reference to row 1, used as an element type |
| `{"initial":1}` | Serialized props passed to the component |
What the client does with it
The client reads the rows as they arrive. For row 1, it asks the bundler runtime to load the chunk and get the default export. Row 0 refers to that module lazily. The client builds a React element whose type is a lazy component. React suspends on it until the chunk loads, then renders Counter with { initial: 1 }.
If the chunk is slow, the surrounding HTML-like content can still appear. A Suspense boundary above the reference controls what shows meanwhile.
Why the bundler matters
The reference only works if the server build and the client build agree on module ids. The framework's bundler plugin produces a manifest that maps each client module to its chunks. The server uses this manifest when writing row 1. The browser uses the same ids to load code. If the manifests drift apart, for example after a partial deploy, the client cannot find the module.
Common errors
- Passing a client function through a server file. Importing a client module and calling it as a plain function on the server fails. Client exports are references, not callable functions.
- Forgetting
'use client'. Without the directive,useStateruns in a Server Component and the build reports an error. - Assuming the reference hides the code. Anything in a client chunk is downloaded by the browser, so never put secrets in it.
Exercise
In the payload row ["$","$L1",null,{"initial":1}], what does $L1 mean, and why does the payload not contain the Counter code?
Show answer
$L1 is a lazy reference to row 1, which describes the client module (path, chunks, export name). The client loads that chunk and uses the export as the element type. The code is not in the payload because Client Components run in the browser and are delivered as ordinary bundled JavaScript chunks. The payload only identifies which one to load.