Sub topic 15
Recoverable errors and client-render fallback
This chapter assumes you can render a page on the server, hydrate it with hydrateRoot, and recognize a hydration mismatch. Here you will see what React does when hydration fails and how to find out that it happened.
What "recoverable" means
When the server HTML and the first client render disagree, React does not leave the page broken. It discards the server-rendered DOM for the affected part of the tree and renders that part again on the client. The user ends up with a working page, but they lose the benefit of the server HTML for that region, and the extra render costs time.
React reports this as a recoverable error. The page keeps working, so nothing in the browser shows that anything went wrong. Without a hook to catch it, the problem can stay unnoticed in production.
Where the fallback happens
The scope of the fallback depends on where the error occurs.
- Inside a Suspense boundary, React falls back to client rendering for that boundary only. The rest of the page stays hydrated.
- Outside any boundary, React falls back to client rendering for the whole root.
- If the server stream aborted or a component threw while streaming, the client also renders that boundary itself, because the server never sent finished HTML for it.
Capturing the errors
Pass onRecoverableError to hydrateRoot. React calls it once for each recovered error.
import { hydrateRoot } from 'react-dom/client';
import App from './App.js';
hydrateRoot(document, <App />, {
onRecoverableError(error, errorInfo) {
console.error('Recoverable error:', error);
console.error('Component stack:', errorInfo.componentStack);
}
});The error describes what failed. The errorInfo.componentStack lists the components that were rendering, which points you at the code to fix.
In production, replace console.error with a call to your monitoring service. A sudden rise in these reports after a deploy usually means a new component reads the time, a random value, or a browser-only API during render.
Server errors versus recoverable errors
These are two different channels. Do not mix them up.
| Where it happens | Callback | Effect |
|---|---|---|
| Server, before the shell is ready | `onShellError` | Send an error page yourself |
| Server, inside a Suspense boundary | `onError` | Client retries that boundary |
| Browser, during hydration | `onRecoverableError` | React re-renders on the client |
An error in a boundary during streaming shows up on the server through onError. The browser then tries to render that boundary and may report its own error through onRecoverableError.
Common mistakes
- Treating a quiet page as a healthy page. A recovered mismatch looks fine to the user but costs a full client render.
- Logging only
error.message. Without the component stack you cannot tell which component to inspect. - Silencing the callback in production. The default only logs to the console, which you will not see from real users.
Exercise
A page has one Suspense boundary around a comments section. A mismatch occurs inside the comments, and nothing else on the page differs. What does React re-render on the client, and how do you learn that it happened?
Show answer
React re-renders only the comments Suspense boundary on the client, and the rest of the page stays hydrated. You learn about it through the onRecoverableError option of hydrateRoot, which receives the error and a component stack.