Sub topic 13
Shell Readiness, Errors, and Timeouts on the Server
This chapter assumes you know renderToPipeableStream basics, Suspense boundaries, and a simple Express server. Here you will learn when to start sending HTML, how to handle errors before and after sending starts, and how to stop a render that takes too long.
The shell
The shell is the part of the page outside any Suspense boundary. React can only start sending HTML once the shell has rendered. Suspense boundaries that are still waiting appear as fallbacks and stream in later.
renderToPipeableStream reports progress through callbacks:
| Callback | When it runs | What to do |
|---|---|---|
| `onShellReady` | The shell has rendered | Set the status and headers, then call `pipe(res)` |
| `onShellError` | The shell failed to render | Send a plain error page |
| `onError` | Any error, before or after the shell | Log it and record that it happened |
A complete route
import express from 'express';
import { renderToPipeableStream } from 'react-dom/server';
import App from './App.js';
const app = express();
app.use('/static', express.static('dist'));
app.get('/', (req, res) => {
let didError = false;
const { pipe, abort } = renderToPipeableStream(<App />, {
bootstrapScripts: ['/static/client.js'],
onShellReady() {
res.statusCode = didError ? 500 : 200;
res.setHeader('Content-Type', 'text/html');
pipe(res);
},
onShellError(error) {
console.error(error);
res.statusCode = 500;
res.setHeader('Content-Type', 'text/html');
res.send('<h1>Something went wrong</h1>');
},
onError(error) {
didError = true;
console.error(error);
}
});
setTimeout(() => abort(), 10000);
});
app.listen(3000);Errors before and after the first byte
The status code is part of the response headers, and headers go out with the first byte. That creates two cases.
- Error before the shell is ready. Nothing has been sent.
onErrorsetsdidError, soonShellReadycan still send a 500. If the shell itself fails,onShellErrorreplaces the whole response with a simple error page. - Error inside a Suspense boundary after streaming starts. The status is already 200 and cannot change. React sends the fallback and tells the browser to retry that boundary on the client.
onErrorstill runs, so you can log the failure.
Timeouts with abort
A slow data source could keep a boundary pending forever. Calling abort() stops waiting. React flushes what it has and leaves unfinished boundaries as fallbacks, and the client then renders them itself. The route above aborts after 10 seconds.
Common mistakes
- Calling
pipeearly, such as atonAllReady. That waits for everything and removes the benefit of streaming. UseonAllReadyonly for crawlers or static generation. - Setting the status after
pipe. The headers have already been sent. - Skipping
onError. Errors inside boundaries are recovered silently, and you will not see them without logging.
Exercise
A component inside a Suspense boundary throws after the shell has already been piped. What status code does the browser receive, and how can you still learn about the error?
Show answer
The browser receives 200, because the headers were sent with the shell and cannot change. React shows the fallback and the client tries to render that boundary. The onError callback still runs on the server, so you can log or report the error there.