Sub topic 5
Reading the RSC payload
This chapter assumes you know React elements and the difference between Server and Client Components. It also assumes you know what the "use client" boundary does. Here you learn to read the stream the server sends to the browser.
What the payload is
The RSC payload is a text stream that describes a React tree. It is not HTML, and it does not contain the code of Server Components. The server runs those components and sends only their output. The stream is also called the React Flight format.
The stream is a series of rows. Each row has an ID, a colon, and a value. The exact syntax differs between React versions and frameworks, so treat the example below as illustrative.
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}]]}]This is the output of the Page component from the earlier chapters, after the server has fetched the posts.
Element rows
Row 0 is an element row, and it holds the root of the tree. Each element is an array of four parts:
["$", type, key, props]"$"marks the array as a React element.typeis a host tag such as"main", or a reference to a component.keyis the React key, ornull.propsis an object. Itschildrenfield holds nested elements, strings, or other values.
If you read ["$","h1",null,{"children":"Posts"}], it is the same as <h1>Posts</h1>. The server has already run Page, so no component function appears in the stream. Only plain elements and data remain.
Client references
Row 1 starts with I, which marks a module reference. Its three values are the module path, the chunk files to load, and the export name. This row tells the browser where the code for Counter lives. The Counter code itself is not in the payload. The bundler produces that code, and the next chapter covers its role.
The element ["$","$L1",null,{"initial":1}] uses "$L1" as its type. This is a lazy reference to row 1. The client reads it as "render the component that row 1 describes." If the chunk has not loaded yet, React waits for it. The props, here {"initial":1}, are plain data, which is why props crossing this boundary must be serializable.
How the references connect
Reading a payload step by step
- Find the root row, usually
0. - Read each
["$", type, key, props]array as an element. - When
typeis a string such as"li", it is a normal DOM element. - When
typestarts with$L, find the row with that ID. - In an
Irow, note the module path and chunk. That is client code the browser must load. - Check the props. Any value you see in the payload was sent to the browser.
Common mistakes
- Looking for Server Component code in the payload. It is never there, only its output.
- Assuming the payload is HTML. The browser cannot display it without React turning it into elements.
- Treating the sample syntax as stable. Inspect the real stream in your framework's network tab, and expect different row types than shown here.
Exercise
In the payload ["$","$L2",null,{"label":"Save"}], what does $L2 mean, and where would you look to find which client file it points to?
Show answer
$L2 is a lazy reference to the row with ID 2. That row should be an I row, whose first value is the module path and whose second value lists the chunks to load. The props {"label":"Save"} are plain serialized data passed to that component.