Sub topic 11
Suspense Boundaries as Streaming Units
This chapter assumes you know function components, useState and useEffect, and how renderToString and hydrateRoot work. It covers how a <Suspense> boundary splits a page into pieces that the server can send at different times.
The problem with all-or-nothing HTML
renderToString returns HTML only when the whole tree is done. If one component needs slow data, such as a list of comments, the browser gets nothing until that data arrives. A single slow part delays the entire page.
A Suspense boundary fixes this. It marks a part of the tree that is allowed to be unfinished. React renders the fallback for that part and moves on.
A boundary around the slow part
import { lazy, Suspense } from 'react';
const Comments = lazy(() => import('./Comments.js'));
export default function Page() {
return (
<main>
<h1>Article</h1>
<Suspense fallback={<p>Loading comments...</p>}>
<Comments />
</Suspense>
</main>
);
}Everything outside the boundary, here <main> and the <h1>, is ready right away. Comments is not. While it is pending, the server sends the fallback in its place. When Comments is ready, the server sends the real HTML as an extra chunk, along with a small inline script that swaps it in for the fallback. No client JavaScript has to run for this swap.
Shell and boundaries
React calls the HTML outside any pending boundary the shell. The shell includes the fallbacks of the boundaries that are still waiting. Each boundary is then a separate streaming unit. It can finish at its own time, and the server sends it when it does.
Choosing where to put boundaries
Placement decides what the user sees early and how much the page shifts later.
| Placement | Effect |
|---|---|
| One boundary around the whole page | The shell is only a fallback. Little benefit over waiting. |
| Boundaries around independent slow sections | Fast content appears first. Each section arrives on its own. |
| A boundary around every small component | Many fallbacks flash and the layout jumps repeatedly. |
A good rule is to wrap sections that are slow and independent, and to give each fallback roughly the size of the real content to limit layout shift.
Common errors
- Suspending outside any boundary. The suspended part then belongs to the shell, so the shell is not ready until it finishes. This brings back the delay you wanted to avoid.
- Fallbacks that differ from content size. The page jumps when the real HTML arrives.
- Expecting
useEffectdata fetching to stream. Effects do not run on the server. Only Suspense-enabled sources, such aslazyor a framework's data API, make a boundary wait.
Exercise
A page has a header, a fast product description, and a slow reviews list. Where should you put a Suspense boundary, and what does the browser receive first?
Show answer
Wrap only the reviews list in a Suspense boundary with a fallback. The browser first receives the shell: the header, the description, and the reviews fallback. The reviews HTML arrives later as a separate chunk, once its data is ready.