Full answer
Classic server rendering (renderToString) is all or nothing. The server waits for all data, sends one big HTML document, and the client must download all JavaScript and hydrate everything before anything is interactive.
Streaming SSR changes this with Suspense. Wrap slow parts in <Suspense fallback={...}>. The server renders the shell, which is everything outside suspended boundaries, and the stream starts. onShellReady fires when the shell is complete, and you pipe the stream to the response then. Each suspended boundary renders its fallback in the shell. When the awaited data resolves, React sends another chunk: a hidden element with the real HTML and an inline script ($RC) that replaces the fallback. No client JavaScript framework code is needed for the swap, so content appears even before hydration.
On the client, hydrateRoot hydrates in pieces. Boundaries hydrate independently, so a slow component does not block the rest. If the user interacts with a boundary that has not hydrated yet, React raises its priority and hydrates it first (selective hydration). Code can be split per boundary with lazy.
Details. onShellError is for failures before the shell is ready (send a 500 or a static fallback), and onError logs errors in boundaries. Errors inside a boundary make React render the client fallback so the client can retry. abort() with a timeout stops waiting for slow data and sends the fallbacks for the rest. Crawlers can be served the fully resolved document with onAllReady instead of onShellReady.
Where data comes from: in a framework such as Next.js, Server Components await data on the server and the framework wires streaming. In a custom setup, use(promise) inside a boundary suspends the render until the promise resolves, and the promise should be created outside the component or cached.
Trade-offs: layout shift if fallbacks differ in size from content, headers and status codes must be decided before streaming starts, and more moving parts than static output.
The example streams a page with one slow boundary and prints what each chunk contained.
import { Suspense, use } from 'react';
import { renderToPipeableStream } from 'react-dom/server';
import { Writable } from 'node:stream';
const reviewsPromise = new Promise((resolve) => setTimeout(() => resolve(['Clear and practical']), 30));
function Reviews() {
const reviews = use(reviewsPromise);
return <ul>{reviews.map((r) => <li key={r}>{r}</li>)}</ul>;
}
function Page() {
return (
<main>
<h1>React Basics</h1>
<Suspense fallback={<p>Loading reviews...</p>}>
<Reviews />
</Suspense>
</main>
);
}
const chunks = [];
const sink = new Writable({
write(chunk, _encoding, done) {
chunks.push(chunk.toString());
done();
},
});
sink.on('finish', () => {
console.log('chunks received: ' + chunks.length);
console.log('first chunk has the fallback: ' + chunks[0].includes('Loading reviews...'));
console.log('first chunk has the review: ' + chunks[0].includes('Clear and practical'));
const rest = chunks.slice(1).join('');
console.log('later chunks have the review: ' + rest.includes('Clear and practical'));
console.log('later chunks have the swap script: ' + rest.includes('$RC'));
});
const { pipe } = renderToPipeableStream(<Page />, {
onShellReady() {
pipe(sink);
},
onShellError(error) {
console.log('shell failed: ' + error.message);
},
});This ran with renderToPipeableStream on the box, outside any framework. Output of the run: chunks received: 2 first chunk has the fallback: true first chunk has the review: false later chunks have the review: true later chunks have the swap script: true
Key points
- The shell is sent first with fallbacks. Resolved boundaries stream later with inline swap scripts.
- onShellReady starts the response. onAllReady waits for everything, for example for crawlers.
- Boundaries hydrate independently and interaction raises the priority of a boundary (selective hydration).
- Handle onShellError, onError and abort timeouts.
- Fallback sizes should match content to limit layout shift.
Common mistakes
- Using renderToString for pages with slow data and blocking the whole response.
- Creating the promise inside the component, so it restarts on every render.
- Forgetting onShellError and returning a broken response.
- Fallbacks with a different size from the content, causing layout shift.
- Setting headers or status codes after the stream has started.
Follow-ups
- What is selective hydration and when does it matter?
- When would you use onAllReady instead of onShellReady?
- How does streaming relate to Next.js loading.js files?
Tips
- Describe the shell and boundary chunks, not only "streaming".
- Mention selective hydration. It is the client-side half of the story.
- Mention the headers and status code limit.