Sub topic 10
Suspense and streaming the payload
This chapter assumes you know how Server Components render into an RSC payload, how "use client" creates client references, and how <Suspense> works in a client-only React app. It covers how Suspense lets the server send the payload in pieces instead of all at once.
The problem: one slow component blocks everything
An async Server Component awaits its data before it can produce output. Suppose a product page has a fast header and a slow reviews list:
// app/Reviews.js (Server Component)
export default async function Reviews() {
const res = await fetch('https://example.com/api/reviews');
const reviews = await res.json();
return (
<ul>
{reviews.map((r) => (
<li key={r.id}>{r.text}</li>
))}
</ul>
);
}Without any boundary, the server cannot finish the root element until Reviews finishes. The browser receives nothing useful until the slowest fetch completes, so one slow query delays the whole page.
Streaming fixes this. The server sends the parts that are ready and marks the unfinished parts as pending. When a pending part finishes, the server sends it as a later piece of the same response.
Suspense as the unit of streaming
A <Suspense> boundary tells React where it may stop waiting and show a fallback instead:
import { Suspense } from 'react';
import Reviews from './Reviews'; // async Server Component
export default function Product() {
return (
<section>
<h1>Product</h1>
<Suspense fallback={<p>Loading reviews...</p>}>
<Reviews />
</Suspense>
</section>
);
}When the server renders this tree, it reaches Reviews, which returns a promise that is not yet resolved. React treats this like a component that suspended. It does not wait. It does two things:
- It emits the surrounding tree (
section,h1, theSuspenseelement with its fallback) with a placeholder where the children should go. - It keeps rendering
Reviewsin the background and emits its output later.
The boundary is therefore the line between "send now" and "send when ready". Content outside any boundary still blocks the first chunk.
What the streamed payload looks like
The payload is a sequence of rows, and each row has an id. Rows do not have to arrive in id order, and a row can refer to an id that has not arrived yet. Here is a simplified, illustrative version of the stream for Product. Exact row syntax differs between React versions and frameworks.
0:["$","section",null,{"children":[["$","h1",null,{"children":"Product"}],["$","$Sreact.suspense",null,{"fallback":["$","p",null,{"children":"Loading reviews..."}],"children":"$L1"}]]}]This is the first chunk. Row 0 is the root tree. "$Sreact.suspense" stands for the Suspense type. The children value "$L1" is a lazy reference. It means "the content for this position is in row 1, which has not been sent yet". The browser can use this chunk right away.
Some time later, the server writes another chunk to the same open response:
1:["$","ul",null,{"children":[["$","li",null,{"children":"Great product"}],["$","li",null,{"children":"Fast delivery"}]]}]Row 1 supplies the element that $L1 pointed to. The response then ends.
The two kinds of reference in a payload are different:
| Reference | Points to | Resolved by |
|---|---|---|
| Client reference (module row, `I[...]`) | Code in a client bundle chunk | The client loading a JavaScript file |
| Lazy reference (`$L1` style) | Another row of the same payload | The server sending that row later |
A lazy reference does not load code. It waits for more payload data on the same stream.
The sequence of events
Notice that the query starts before chunk 1 is sent. Streaming does not delay the work. It only avoids waiting for it before sending what is ready.
What the client does with each chunk
The client-side React runtime reads the response body as a stream and processes rows as they arrive.
- When a row arrives that a lazy reference was waiting on, React resolves that reference. Internally this behaves like a promise that was pending and is now fulfilled.
- The
Suspenseboundary on the client was suspended because its child was an unresolved reference. When the reference resolves, React re-renders the boundary and swaps the fallback for the real content. - Only the boundary updates. The heading and anything outside the boundary are not remounted, so state in client components outside the boundary is kept.
The client never has to know what a Server Component is. It only sees elements, client references and lazy references.
Nested boundaries and ordering
Boundaries can nest. Each one is an independent streaming point:
import { Suspense } from 'react';
import Reviews from './Reviews';
import Recommendations from './Recommendations';
export default function Product() {
return (
<section>
<h1>Product</h1>
<Suspense fallback={<p>Loading reviews...</p>}>
<Reviews />
</Suspense>
<Suspense fallback={<p>Loading recommendations...</p>}>
<Recommendations />
</Suspense>
</section>
);
}The two components render concurrently on the server. Whichever finishes first is streamed first, so row 2 may arrive before row 1. Because rows carry ids, order of arrival does not matter. Each reference resolves when its own row shows up.
Place boundaries by asking which pieces can appear independently. A boundary around every list item produces many tiny fallbacks and layout jumps. A single boundary around the whole page gives you back the all-or-nothing behavior.
Waterfalls still happen
Suspense does not make data fetching parallel for you. Compare these two:
// Sequential: the second fetch starts only after the first finishes
export default async function Page() {
const user = await getUser();
const posts = await getPosts(user.id);
return <Feed user={user} posts={posts} />;
}// Independent: both start immediately
export default async function Page() {
const [user, posts] = await Promise.all([getUser(), getPosts()]);
return <Feed user={user} posts={posts} />;
}The first is correct when getPosts really needs user.id. If it does not, use the second form, or move each fetch into its own async component under its own Suspense boundary. Components that are siblings start rendering together. A child component starts only after its parent has rendered, so fetching in a child that sits under a parent that is itself awaiting creates a waterfall.
Errors and status codes
Streaming changes how failures behave, and two cases catch people out.
An error inside a boundary after the first chunk. The server has already sent a successful response. It cannot change the HTTP status to 500. Instead, React encodes the error in the payload (typically with a digest, not the full message in production) and the client treats the boundary as having thrown. The nearest error boundary on the client shows its fallback. Wrap risky boundaries in an error boundary or use your framework's error file convention.
An error outside any boundary, before the first chunk. The server has not sent anything yet, so the framework can still return an error status and an error page.
Fallbacks must be cheap and serializable
The fallback is part of the first chunk, so it follows the same serialization rules as any other props. Keep it light: a skeleton or a short message. A fallback that itself fetches data would defeat the purpose.
Streaming and the SSR HTML
If your framework also server-renders HTML (covered in the next chapter), Suspense streaming applies to the HTML as well. The HTML first shows the fallback, then later HTML for the resolved content arrives with a small script that swaps it in. The RSC payload is streamed alongside the HTML so the client can hydrate. The two streams are separate formats that follow the same boundaries.
Common mistakes
- No boundary around a slow component. The whole route waits. Add a boundary, or accept the wait deliberately.
- Boundary too high in the tree. The fallback replaces large parts of the page that were ready.
- Fetching in nested children sequentially. This causes waterfalls that boundaries do not remove.
- Relying on a redirect or
notFoundfrom inside a boundary. The status is already sent. - Expecting a fixed arrival order. Rows resolve by id, not by position.
Exercise
A page renders a header, then <Suspense fallback={<Skeleton />}><Comments /></Suspense>, where Comments is an async Server Component that takes 3 seconds. Describe what the first chunk of the payload contains for the Suspense position, what the browser shows at 1 second, and what the server sends at 3 seconds.
Show answer
The first chunk contains the header elements and the Suspense element with its fallback (Skeleton) as a prop, and the children position holds a lazy reference such as "$L1" because row 1 is not ready. At 1 second the browser shows the header and the skeleton, and the Suspense boundary on the client is suspended on that unresolved reference. At 3 seconds the server sends row 1 containing the rendered Comments output. The client resolves $L1, re-renders only the boundary, and replaces the skeleton with the comments. The header is not remounted.
Exercise
A Reviews component inside a Suspense boundary throws an error after the page has already started streaming. Can the server respond with HTTP 500, and where does the failure appear?
Show answer
No. The status line and headers were sent with the first chunk, so the status stays 200. The error is encoded in the payload for that boundary's row, and on the client the nearest error boundary renders its fallback in place of the reviews. The rest of the page stays visible.