Sub topic 14
Selective hydration of Suspense boundaries
This chapter assumes you have read the earlier chapters on hydrateRoot, Suspense boundaries, and renderToPipeableStream. You should know that hydration attaches React to server-rendered HTML, and that a <Suspense> boundary is a unit the server can send later than the rest of the page.
The problem selective hydration solves
Before React 18, hydration was all or nothing. The browser had to download the JavaScript for the whole page. React then hydrated the whole tree in one pass, and only after that could the user interact with anything. One slow component, such as a large comments widget, delayed interactivity for everything, including a button in the header.
Selective hydration removes that constraint. It lets React hydrate a Suspense boundary on its own schedule and treats each boundary as a separate piece of work. It has three effects:
- A boundary whose code or HTML has not arrived does not block hydration of the rest of the page.
- Hydration is split into small slices, so the browser can handle input between them.
- When the user interacts with a part of the page that is not hydrated yet, React hydrates that part first.
You get this by wrapping slow or low-priority parts of the page in <Suspense>. React has no separate API for it.
A page with independent boundaries
import { lazy, Suspense } from 'react';
const Comments = lazy(() => import('./Comments.js'));
const Recommendations = lazy(() => import('./Recommendations.js'));
export default function Page() {
return (
<main>
<h1>Article</h1>
<p>The article text is plain markup and hydrates with the shell.</p>
<Suspense fallback={<p>Loading comments...</p>}>
<Comments />
</Suspense>
<Suspense fallback={<p>Loading recommendations...</p>}>
<Recommendations />
</Suspense>
</main>
);
}The client entry is the same as in the earlier chapters:
import { hydrateRoot } from 'react-dom/client';
import App from './App.js';
hydrateRoot(document, <App />);Nothing here mentions selective hydration. React hydrates the shell (main, the heading, the paragraph) first. It then treats Comments and Recommendations as separate pending units. Each one hydrates when its own JavaScript is loaded and its own HTML is present in the document.
What React does, step by step
- The browser parses the streamed HTML. The shell and any boundaries that were ready are already in the document.
client.jsloads andhydrateRootstarts. React hydrates the shell and every boundary that is ready, in slices.- For a boundary whose lazy chunk has not loaded, React leaves the server HTML alone and keeps it visible. If the server had not finished that boundary, the fallback HTML stays on screen instead.
- When the chunk arrives, React schedules the boundary's hydration as a new task.
- If the user clicks inside a boundary that is still waiting, React raises that boundary's priority and hydrates it next.
Events that arrive before hydration
React attaches its event listeners at the root, so it sees clicks on server HTML even where no component has hydrated. Its behavior depends on the event type:
- Discrete events such as clicks, key presses, and form input are recorded. React hydrates the target boundary at high priority and then replays the event, so the handler runs as if the component had been interactive all along.
- Continuous events such as hovers and scrolling are not replayed. They only raise the priority of the boundary under the pointer.
Ancestors hydrate before descendants, because a child needs its parent's context and props. A click in a boundary nested inside another boundary therefore hydrates the outer boundary first, then the inner one.
import { Suspense } from 'react';
import Comments from './Comments.js';
import ReplyForm from './ReplyForm.js';
export default function Discussion() {
return (
<Suspense fallback={<p>Loading discussion...</p>}>
<Comments />
<Suspense fallback={<p>Loading reply form...</p>}>
<ReplyForm />
</Suspense>
</Suspense>
);
}A click on the reply form hydrates Comments and the outer boundary first, then ReplyForm. If the form should be usable sooner, move it out of the outer boundary.
Choosing where to put boundaries
Boundary placement decides what hydrates independently. Use these criteria:
| Decision | Guidance |
|---|---|
| Boundary around heavy, secondary content (comments, recommendations, charts) | Good. The rest of the page becomes interactive without waiting for it. |
| One boundary around the whole page | Little benefit. The page still hydrates as a single unit. |
| A boundary around every small component | Extra markers in the HTML and more loading states for users to see. Add boundaries where the code or data is slow. |
| Interactive controls the user needs immediately (nav, search box) | Keep them in the shell, outside any boundary, so they hydrate first. |
A boundary helps most when the content inside it is large, loaded through lazy, or waits on data. It matters less for content that arrives with the shell and is cheap to hydrate.
What selective hydration does not change
- It does not reduce the total JavaScript the browser must download. It reorders and splits the work. Code that never ships to the client is a separate topic, covered by Server Components later in this tutorial.
- It does not make a mismatch harmless. Hydration inside a boundary is still checked against the server HTML.
- It needs
hydrateRootand the streaming server APIs. Pages rendered withrenderToStringand attached with the legacyReactDOM.hydratedo not hydrate selectively.
Errors and mismatches inside a boundary
Because boundaries are independent units, failures are contained to them:
- If the server fails to render a boundary, the stream keeps the fallback HTML for that boundary and the client tries to render the content itself. The shell still hydrates normally.
- If a mismatch is found while hydrating a boundary, React discards that boundary's server HTML and renders it from scratch on the client. The rest of the page stays hydrated. React reports this through
onRecoverableError.
The next chapter covers how to capture and log these recoverable errors. The practical consequence for this chapter is that one broken widget no longer forces the whole page to re-render on the client, as long as it sits inside its own boundary.
Common mistakes
- Expecting interactivity from a boundary whose fallback is still showing. The fallback is plain HTML, so nothing in it responds until the real content has streamed in and hydrated.
- Wrapping the entire app in one
<Suspense>and assuming everything hydrates in parallel. It hydrates as one unit. - Placing a boundary inside the shell but loading its code from the main bundle. The code is then already present, so the boundary hydrates almost immediately and the split gains little.
- Reading the order of hydration from the order in the source file. React orders work by priority, which depends on readiness and user input.
Exercise
A page has a header with a search box in the shell, and a heavy Chart component wrapped in its own <Suspense> boundary and loaded with lazy. A user types in the search box while the Chart chunk is still downloading. Does the search box wait for the chart? Then the user clicks a button inside the chart before its chunk arrives. What happens to the click?
Show answer
The search box does not wait. It is in the shell, which hydrates without the Chart boundary, so typing works once the shell has hydrated. The click inside the chart is a discrete event, so React records it, raises the priority of the Chart boundary, and hydrates it as soon as its chunk arrives. Then it replays the click so the handler runs.