Sub topic 5
Reading the RSC payload
This chapter shows you how to read an RSC payload by hand. You will learn what a row is, what the markers inside it mean, and how a streamed payload fills in a tree over time. It assumes you know React elements and components, async/await, and the difference between Server and Client Components, including what "use client" does. The format shown here is a simplified view of React's Flight protocol. Row syntax and markers change between React versions and frameworks, so use this chapter to learn the model and not as a parser specification.
What the payload is
The payload is the serialized result of rendering Server Components. It is not HTML, and it is not JavaScript for the server parts. It describes a React element tree in a text-based format, together with pointers to the client code that must run in the browser.
Three facts make it readable:
- Server Components have already run. Their output appears as plain elements such as
main,h1andli. Their code does not appear. - Client Components have not run. They appear as references to a module, plus the props they will receive.
- The payload is a stream of rows. The server can write rows as they become ready, and the client can start reading before the stream ends.
Anatomy of a row
Each row is one line of the form id:data. The id is a row number in hexadecimal. The data after the colon is usually JSON, sometimes with a type prefix before it.
Here is the payload produced by the Page and Counter examples earlier in this tutorial, with one post:
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 0 is the root of the tree. Row 1 is a module reference, marked by the I before its data. The order matters for reading, not for meaning. The client can process the rows in any order if it keeps them in a table keyed by id.
| Part | Example | Meaning |
|---|---|---|
| Row id | `0` | Hex number that other rows use to refer to this row |
| Row type | `I` | Module reference (an import). Rows with no letter hold a value, usually an element tree |
| Module metadata | `["app/Counter.js",["chunk-counter.js"],"default"]` | Module id, the JavaScript chunks to load, and the export name |
Reading an element
An element is a four-item JSON array:
["$", "li", null, {"children": "First post"}]"$"is a marker that says "this array is a React element, not a plain list".- The type is
"li", a host (HTML) element. - The key is
null, because no key was given. - The props object holds
childrenand any other props.
This mirrors the object that React.createElement("li", null, "First post") produces: a type, a key and props. Arrays that do not start with "$" are ordinary lists, such as the array of children in the example.
Since elements are plain data, the payload contains no component functions. If Page called a helper component named PostList, you would see only the ul and li elements it returned. Server Component boundaries leave no trace in the output.
Reading references
The payload uses strings that begin with $ as markers. A string that starts with $ is a pointer or a special value. The character after $ tells you which one.
| Marker | Example | Meaning |
|---|---|---|
| `$` followed by a hex id | `"$2a"` | Reference to the value in row `2a` |
| `$L` followed by an id | `"$L1"` | Lazy reference. The value may not have arrived or loaded yet |
| `$@` followed by an id | `"$@3"` | A promise whose result comes from row `3` |
| `$undefined` | `"$undefined"` | The value `undefined`, which JSON cannot express |
| `$S` followed by a name | `"$Sreact.suspense"` | A well-known symbol, here the Suspense type |
| `$$` at the start | `"$$5 dollars"` | Escape for a real string that begins with `$` |
Only the first three are needed for most reading. The last row shows why a plain string needs escaping: without it, a user's text such as "$L1" could be mistaken for a reference.
Client references
Look again at the element for Counter:
["$","$L1",null,{"initial":1}]The type is "$L1", not a string like "button". That is a lazy reference to row 1, the module reference for app/Counter.js. To render this element, the client does the following:
- Looks up row
1and finds the module id, the chunk list and the export name (default). - Loads
chunk-counter.jsif it is not already loaded. - Takes the
defaultexport of that module and uses it as the element type. - Renders it with the props
{"initial":1}.
The reference is lazy because the chunk may still be loading when the parser reaches the element. React can hold a placeholder and finish the element once the code arrives, without blocking the rest of the tree.
Row 1 exists because Counter.js begins with "use client". The bundler replaced that module, on the server side, with a reference that carries the module id. This is how the server can name a component without ever running it or importing its code.
flowchart LRReading a streamed payload
Rows do not have to arrive together. With a Suspense boundary, the server can send the surrounding content first and the slow part later. Take the Product page from the Suspense example. A simplified payload might look like this:
0:["$","section",null,{"children":[["$","h1",null,{"children":"Product"}],["$","$Sreact.suspense",null,{"fallback":["$","p",null,{"children":"Loading reviews..."}],"children":"$L2"}]]}]
2:["$","ul",null,{"children":[["$","li",null,{"children":"Great product"}]]}]The first chunk of the stream contains only row 0. Its Suspense element has a fallback that is fully described, and a children value of "$L2", which points to a row that does not exist yet. The client renders the heading and the "Loading reviews..." text. When row 2 arrives later, the client resolves the reference and React swaps the fallback for the reviews.
Reading rule: a reference to a row you have not seen is not an error. It means "this part is still coming".
Passing data to Client Components
Props on a client element are serialized into the same payload. In the first example, initial is the number 1. Any value you pass must be something the format can express, which includes strings, numbers, booleans, null, arrays, plain objects, and some built-ins such as Date, Map, Set and promises. A function cannot be written into a row, so passing one fails at render time. The next chapter in the plan covers these rules in detail. For now, use them as a reading aid: every prop you see in a payload is data, never behavior.
Inspecting a real payload
Frameworks that use RSC send payloads over the network, so you can look at them in the browser's developer tools.
- Open the Network tab and reload the page. Some frameworks embed the initial payload in the HTML, inside script tags, and others fetch it separately.
- Navigate to another route using a link. Look for a request that returns text made of lines like
0:.... Many frameworks mark such requests with a header or a query parameter. - Open the response and search for
$Lto find client references, and forI[to find module rows.
The exact format will not match this chapter character for character. Newer React versions add row types, change the module metadata layout, and use different tags for things like errors and hints. If a row does not match, compare the structure (id, type letter, data) and not the specific characters.
Common reading mistakes
- Looking for Server Component code. It is not in the payload. Only the elements it returned are.
- Treating
$L1as a string. It is a reference. Read row1to learn what it points to. - Assuming row
0is always first in the stream. It is the root, but the order of rows on the wire depends on when each part is ready. - Expecting rows to be complete JSON documents. In some formats, row data carries a type prefix or tag before the JSON, so a parser has to read the tag first.
Exercise
A payload contains the row 4:I["app/Chart.js",["chunk-chart.js"],"default"] and an element ["$","$L4",null,{"data":[1,2,3]}]. What will the client do to render that element, and which code runs on the server for Chart?
Show answer
The client looks up row 4, loads chunk-chart.js if needed, takes the default export of app/Chart.js, and renders it with the props {"data":[1,2,3]}. No Chart code runs on the server. The server only wrote the reference and the serialized props, because Chart.js is a Client Component module.
Exercise
Row 0 of a streamed payload contains "children":"$L7" inside a Suspense element, but no row 7 has arrived. What does the browser show, and what happens when row 7 arrives?
Show answer
The browser shows the Suspense fallback that row 0 describes. When row 7 arrives, the client resolves the reference, and React replaces the fallback with the content from that row.