Sub topic 10
Suspense and streaming the payload
This chapter assumes you know async/await, basic React components, and the idea of a Server Component that awaits data. It also assumes you have read how the RSC payload is structured, with element rows and client references.
The problem: one slow query blocks everything
A Server Component can be an async function. If a page awaits three queries, the server cannot finish the page until the slowest query returns. Without any boundaries, the browser sees nothing until the whole payload is ready.
Suspense fixes this. A <Suspense> boundary tells React that the content inside it may not be ready yet, and gives it a fallback to show in the meantime.
A Suspense boundary on the server
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>
);
}Product itself is not async, so it renders at once. Reviews awaits its data. When React reaches Reviews and its promise is still pending, it does not wait. It emits the fallback in that spot and moves on.
How the payload is sent in pieces
The RSC payload is a stream of rows, not one document. Each row can be written as soon as it is ready. For the example above, the server sends:
- A first chunk with the
section, theh1, and the fallback paragraph. The slot whereReviewswill go is marked with a reference to a row that does not exist yet. - Later, when
Reviewsfinishes, a new row with its rendered output.
The following diagram shows the order of events.
A simplified view of the stream looks like this. The exact 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"}]]}]
1:["$","ul",null,{"children":[["$","li",null,{"children":"Great product"}]]}]In row 0, the Suspense element's children is "$L1". This is a lazy reference. It means "the content for this spot is in row 1, which has not arrived yet". When row 1 arrives, the client fills the slot, and React swaps the fallback for the real content.
What the client does with it
The client reads the stream incrementally. It does not wait for the end. Each row it finishes parsing can be applied to the tree. Because the lazy reference sits inside a Suspense boundary, React treats the unresolved part as "suspended" and shows the fallback until the row resolves.
Only the content inside the boundary updates. The h1 and everything outside it stay as they were, and so does any client state in components outside the boundary.
Choosing boundary placement
- Wrap each independently slow section in its own boundary. Two boundaries let two slow queries resolve in either order.
- Do not wrap everything in one boundary. The fallback then covers the whole page, and you lose most of the benefit.
- Start slow requests early. If one component awaits sequentially after another, the boundaries do not make them parallel.
Common errors
- Putting
awaitin the parent that renders the boundary. The parent then blocks before it can emit anything, so the fallback never appears. Put theawaitinside the child. - Expecting a fallback for data fetched in a Client Component's
useEffect. That is client fetching, and Suspense does not track it. - Forgetting that an error inside the boundary needs an error boundary, or the failure can affect the surrounding page.
Exercise
A page awaits a slow query at the top of its async Server Component, then renders a Suspense boundary around a child. Why does the fallback never show, and how do you fix it?
Show answer
The parent awaits the query before it returns any elements, so the server cannot emit the shell or the fallback until the query finishes. Move the await into the child component inside the boundary, so the parent returns at once and the child's pending promise triggers the fallback.