Sub topic 15
Recoverable errors and client-render fallback
This chapter assumes you can already render a page with renderToPipeableStream and attach React with hydrateRoot. It also assumes you know what a Suspense boundary is and why a hydration mismatch happens. Here you will learn what React does after something goes wrong, how far the damage spreads, and how to see it in production.
What "recoverable" means
React does not stop when server HTML and the client's first render disagree. It throws the server HTML away for the affected part of the tree and renders that part again in the browser. The page still works, but you lose the benefit of server rendering for that part. React calls the errors behind this behavior recoverable because the user can still use the page.
Two situations lead here:
- A hydration mismatch. The client's first render produces a different tree than the server HTML contains.
- A server error inside a Suspense boundary. The shell was already sent, so the server cannot change the status code. It sends the boundary's fallback and leaves the rendering to the client.
A third situation does not lead here. If the shell fails (the part outside every Suspense boundary), onShellError runs and you send an error page yourself. Nothing is streamed, so there is nothing to recover.
How far the fallback spreads
React discards as little as it can. It looks for the nearest Suspense boundary above the failing component.
| Where the problem occurs | What React re-renders on the client |
|---|---|
| Inside a Suspense boundary | Only that boundary's content. The rest of the page stays hydrated. |
| Outside any Suspense boundary | The whole root, as in a plain client-rendered app. |
| Inside nested boundaries | The nearest enclosing boundary of the failing component. |
This is why Suspense boundaries matter for more than streaming. A boundary around a risky widget limits a mismatch to that widget. Without it, one mismatched timestamp in a footer can force the whole document to render again.
The server side of the fallback
Suppose a component throws during server rendering while it sits inside a boundary:
import { Suspense } from 'react';
function Comments() {
if (typeof window === 'undefined') {
throw new Error('Comments failed on the server');
}
return <ul><li>First comment</li></ul>;
}
export default function Page() {
return (
<main>
<h1>Article</h1>
<Suspense fallback={<p>Loading comments...</p>}>
<Comments />
</Suspense>
</main>
);
}Three things happen:
- The
onErrorcallback ofrenderToPipeableStreamruns with the error. Log it there. - The server sends the article and the fallback
<p>Loading comments...</p>.onShellReadystill fires and the status stays 200. - In the browser, React sees that the server could not finish the boundary. It renders
Commentson the client. If that render succeeds, the comments appear. If it throws too, the error goes to the nearest error boundary.
The first request that hits this fallback is a normal 200 response. So onError is the only signal the server gives you that something failed. In the Express example from the streaming chapter, didError is set in onError. Because onShellReady has already sent headers by the time a later boundary fails, that flag only changes the status code for errors that occur before the shell is ready.
Capturing recoverable errors in the browser
React reports each recovered error through the onRecoverableError option of hydrateRoot. If you do not pass it, React reports the error with the browser's default error reporting, which usually means a console message. That is easy to miss, and it never reaches your monitoring. A recovered mismatch is invisible to users apart from a slower page, so you need to log it yourself.
import { hydrateRoot } from 'react-dom/client';
import App from './App.js';
function report(error, errorInfo) {
const payload = JSON.stringify({
message: String(error && error.message),
stack: error && error.stack,
componentStack: errorInfo && errorInfo.componentStack,
url: window.location.href
});
const body = new Blob([payload], { type: 'application/json' });
navigator.sendBeacon('/log/hydration', body);
}
hydrateRoot(document, <App />, {
onRecoverableError(error, errorInfo) {
console.error('Recoverable error:', error);
report(error, errorInfo);
}
});navigator.sendBeacon is a good fit because it queues the request without blocking the page and survives a navigation. Your server needs a /log/hydration route that accepts a POST with a JSON body.
Several details are worth knowing:
- The callback runs once per recovered error, not once per page. A page with three failing boundaries can report three times.
errorInfo.componentStacklists the components above the failure. It is the most useful field for finding the culprit.- Messages differ between React versions. A server failure usually reads like "The server could not finish this Suspense boundary, likely due to an error during server rendering. Switched to client rendering." Mismatch messages describe the difference in text or attributes. Group your logs by component stack rather than by exact message text.
- In some versions, the original server error is attached as
error.cause. Check for it but do not depend on it.
What the fallback costs
A recovered error is cheap to ignore and expensive to leave in place.
- Wasted work. The server rendered HTML that the browser discards. The client then renders the same part again.
- Layout shift. The replaced content can differ from what the user was reading. Text may jump or an input may lose focus.
- Slower interaction. A whole-root fallback gives you the timing of a client-rendered app, with the extra cost of downloading HTML you do not use.
- Hidden bugs. Because the page works, nobody notices until a metric drops.
Common mistakes
- Swallowing the callback. An
onRecoverableErrorthat does nothing hides every fallback. Always log at least the component stack. - Expecting a 500 status. A boundary error after the shell is sent cannot change the status code. Rely on
onErrorfor server-side alerts. - No boundaries around risky content. Without a Suspense boundary, a small mismatch re-renders the whole root. Wrap content that depends on time, locale, or third-party data.
- Confusing error boundaries and Suspense boundaries. A Suspense boundary decides what React re-renders on the client. An error boundary catches errors thrown by the client render itself. A boundary error on the server does not reach an error boundary unless the client render throws too.
- Testing only in development. Development builds print extra mismatch details that production builds omit. Check your production logging path with a deliberately broken component.
Exercise
A page has a footer outside any Suspense boundary that prints the current time during render, and a comments section inside a Suspense boundary that renders fine. The footer causes a hydration mismatch. What does React re-render on the client, and which callback should you use to find out it happened?
Show answer
The footer is outside every boundary, so React discards the server HTML for the whole root and renders the entire page on the client. Pass onRecoverableError to hydrateRoot to receive the error and its component stack. The fix is to render a stable placeholder first and set the time in an effect, or to wrap the footer in its own Suspense boundary to limit the damage.
Exercise
A component inside a Suspense boundary throws during server rendering. The browser still shows the page, and the response status is 200. Where on the server can you detect the failure, and what does the browser do with the boundary?
Show answer
The onError callback of renderToPipeableStream receives the error, so log or alert there. The server sends the boundary's fallback, and the browser tries to render the boundary's content on the client. If that render also throws, the nearest error boundary handles it.