Sub topic 11
Suspense boundaries as streaming units
This chapter assumes you know function components, useState and useEffect, and you've seen the server render and hydrateRoot flow from earlier chapters. It covers how <Suspense> splits a page into parts that the server can send at different times. The next chapter covers the server API that does the sending.
What a boundary does on the server
In the browser, <Suspense> shows a fallback while something inside it is not ready. On the server it does the same job, with one difference. The server cannot show one thing and then change it in place, so it sends the fallback first and sends the real content later as a separate piece of the same HTML response.
A Suspense boundary is a <Suspense> element and everything inside it. React treats each boundary as an independent unit with three possible states: pending, complete, or errored. The server does not wait for a pending boundary before it sends the rest of the page.
Code can suspend in three common ways:
React.lazyloads a component's module the first time it renders.- A Suspense-enabled data source, such as one built into a framework or a library that supports Suspense, throws a pending promise while data loads.
- The
usehook, in React versions that have it, reads a promise during render.
Fetching inside useEffect does not suspend. Effects do not run during server rendering, so Suspense can't wait for them.
The shell and the late pieces
The shell is the HTML React can produce without waiting on any boundary. It includes your layout, headings, and the fallbacks of every boundary that is still pending. Everything else arrives later.
The example below has one boundary around the comments.
// Page.js
import { lazy, Suspense } from 'react';
const Comments = lazy(() => import('./Comments.js'));
export default function Page() {
return (
<main>
<h1>Article</h1>
<p>The article text is available immediately.</p>
<Suspense fallback={<p>Loading comments...</p>}>
<Comments />
</Suspense>
</main>
);
}// Comments.js
export default function Comments() {
return (
<ul>
<li>First comment</li>
<li>Second comment</li>
</ul>
);
}When the server renders Page, Comments suspends because its module is not loaded yet. The shell therefore contains the heading, the paragraph, and <p>Loading comments...</p>. Once the module loads, React renders Comments and appends its HTML to the stream, followed by a small inline script. That script moves the new HTML into the place where the fallback was. The browser shows the swap even before any of your JavaScript has loaded.
The sequence looks like this.
Where the shell ends
Only suspending components that sit inside a boundary can be streamed later. If a component suspends and no boundary above it catches the suspension, the shell itself is not ready until that component finishes. The first byte of the page then waits on it.
import { Suspense } from 'react';
export default function Layout({ children }) {
return (
<html lang="en">
<body>
<Nav />
<Suspense fallback={<p>Loading page...</p>}>{children}</Suspense>
</body>
</html>
);
}Here the Suspense element around children means a slow page body cannot hold back <Nav />. Without it, any slow descendant would delay the whole response.
Nested boundaries
Boundaries can contain other boundaries. Each one completes on its own timeline.
import { Suspense } from 'react';
import Sidebar from './Sidebar.js';
import Feed from './Feed.js';
import Comments from './Comments.js';
export default function Dashboard() {
return (
<Suspense fallback={<p>Loading dashboard...</p>}>
<Sidebar />
<Feed />
<Suspense fallback={<p>Loading comments...</p>}>
<Comments />
</Suspense>
</Suspense>
);
}The outer boundary stays on its fallback until Sidebar and Feed can render. At that point the outer content replaces its fallback, and the inner boundary may still show Loading comments.... The comments then arrive as their own piece. Siblings work the same way. Two boundaries side by side can reveal in either order, depending on which finishes first.
Choosing where to put boundaries
A boundary is a decision about what the user sees first and what is allowed to wait. These are the trade-offs.
| Placement | Benefit | Cost |
|---|---|---|
| One boundary around the whole page | Simple, and the page appears in one step | One slow part delays everything below the fallback |
| A boundary per slow region | Fast regions appear early | More fallbacks to design, and content may pop in at different times |
| A boundary around every small component | Maximum independence | Layout shifts, visual noise, and more streamed chunks |
Some guidelines that hold up in practice:
- Wrap parts that are slow or optional, such as comments, recommendations, or a personalized panel. Leave critical content such as the title and main text outside.
- Give fallbacks roughly the same size as the final content. A tall comments list replacing a one-line fallback pushes everything below it down.
- Group content that should appear together. If two panels would look wrong with one loaded and the other missing, put them in one boundary.
- Do not put the main content, which search engines and link previews need, behind a boundary you expect to be slow.
Boundaries and hydration
Boundaries also matter after the HTML reaches the browser. Because each boundary is a separate unit, React can hydrate them separately instead of treating the page as a single block. A boundary whose code has not loaded yet does not stop the rest of the page from becoming interactive. The chapter on selective hydration covers this in detail. For now, remember that the boundaries you place for streaming also become the units for hydration.
Errors inside a boundary
If a component inside a boundary throws while the server renders, React does not fail the whole response. It leaves that boundary's fallback in the HTML and tries to render the content again in the browser. If that also fails, the nearest error boundary on the client takes over. An error outside every boundary is different: it prevents the shell from being produced. Handling both cases on the server is the subject of the lifecycle chapter.
Common mistakes
- Expecting effects to suspend. Data fetched in
useEffectnever runs on the server, so the server HTML contains whatever the component renders before the data arrives. - Putting
<Suspense>too low. If the boundary is inside the slow component instead of around it, the component still suspends above the boundary and delays the shell. - Using a fallback with no reserved space. The streamed content shifts the layout when it arrives.
- Wrapping everything. Many tiny boundaries make the page load in visible stages.
Exercise
A page renders a header, a product description, and a reviews list that loads slowly. The reviews component suspends, and the page has no <Suspense> element. What does the user wait for, and what change fixes it?
Show answer
Because the reviews component suspends and no boundary catches it, the shell is not ready until the reviews finish, so the header and description are held back too. Wrapping the reviews component in <Suspense fallback={...}> lets the server send the header, the description, and the fallback immediately, and stream the reviews when they are ready.
Exercise
Inside <Suspense fallback={<p>Loading...</p>}>, a component fetches data in useEffect and renders "No data" until the fetch finishes. Will the boundary show the fallback while the server waits for that data? Why or why not?
Show answer
No. Effects do not run during server rendering and a pending effect does not suspend the component. The server renders the component once with its initial state and sends "No data" in the HTML. To stream the data, use a Suspense-enabled data source or the use hook so the component suspends during render.