Streaming SSR with React DOM server APIs
Streaming SSR lets React send useful HTML progressively instead of waiting for the entire server tree to finish before sending the first byte.
The streaming pipelineโ
The benefit is progressive delivery, not faster databases or APIs.
Server API familiesโ
renderToReadableStream
Produces a Web ReadableStream.
renderToPipeableStream
Produces a Node pipeable stream interface.
Node also exposes Web Stream compatibility APIs, but the dedicated Node server APIs are the intended performance path for Node environments.
renderToPipeableStreamโ
import { renderToPipeableStream } from 'react-dom/server';
function handleRequest(req, res) {
let didError = false;
const { pipe, abort } = renderToPipeableStream(<App />, {
bootstrapScripts: ['/main.js'],
onShellReady() {
res.statusCode = didError ? 500 : 200;
res.setHeader('content-type', 'text/html');
pipe(res);
},
onShellError(error) {
console.error(error);
res.statusCode = 500;
res.end('<h1>Server error</h1>');
},
onError(error) {
didError = true;
console.error(error);
},
});
setTimeout(() => abort(), 10000);
}
This single example exposes the major production boundaries: shell readiness, status/header timing, fatal shell failure, observability, and abort policy.
What is the shell?โ
function App() {
return (
<html>
<body>
<Header />
<Suspense fallback={<FeedSkeleton />}>
<Feed />
</Suspense>
</body>
</html>
);
}
If Feed suspends, React can still produce document structure, header, fallback UI, and bootstrap resources.
onShellReady vs onAllReadyโ
Start streaming when the shell is usable.
Better time-to-first-content, later boundaries continue arriving.
Wait until all rendering completes before piping.
Useful for special complete-output cases, but removes most progressive benefit.
Once bytes are sent, HTTP status and headers may already be committed. Decide required status/headers before starting the stream.
Shell failure vs later boundary failureโ
onShellError can return a completely different server response and status.Use onError for logging, tracing, request IDs, and status decisions that are still possible before the shell is emitted.
Suspense enables progressive HTMLโ
<Suspense fallback={<ProfileSkeleton />}>
<Profile />
</Suspense>
Non-streaming vs streaming waterfallโ
Fetch everything โ render everything โ send HTML โ load JS โ hydrate
Render shell โ send shell โ continue server work โ stream completed regions โ hydrate progressively
Streaming changes when useful output arrives. It does not make a five-second query complete in one second.
Avoid server waterfallsโ
Bad architecture serializes independent work:
When dependencies allow, start independent work early and let meaningful Suspense regions resolve independently.
Abort and request lifetimeโ
const { abort } = renderToPipeableStream(<App />, options);
setTimeout(() => {
abort();
}, 10000);
A render should not consume server resources indefinitely.
Connect client disconnect/cancellation to rendering and data work where the runtime/framework supports it.
renderToReadableStreamโ
Web Stream runtimes use the Promise-based API:
import { renderToReadableStream } from 'react-dom/server';
const stream = await renderToReadableStream(<App />, {
bootstrapScripts: ['/main.js'],
});
return new Response(stream, {
headers: {
'content-type': 'text/html',
},
});
The server runtime decides how that stream becomes an HTTP response.
Bootstrap scripts and hydrationโ
Server HTML is not automatically interactive.
renderToPipeableStream(<App />, {
bootstrapScripts: ['/main.js'],
});
A real framework/bundler usually owns hashed asset URLs. Avoid hardcoding generated production filenames.
A page can stream excellent HTML and still need JavaScript for interactive Client Components.
Status-code trade-offโ
Framework conventions often make not-found/error/status behavior safer than hand-rolling this logic.
SSR and Server Components are different axesโ
SSR asks how output reaches the browser as HTML. Server Components ask where component code executes and what crosses boundaries.
Legacy string APIsโ
renderToString still exists for compatibility/non-streaming cases, but modern Suspense-heavy applications generally benefit from framework-supported streaming or the current streaming APIs.
renderToStaticMarkup is for non-interactive HTML and is covered with static rendering.
Production checklistโ
- identify a meaningful shell;
- place slow independent UI behind meaningful Suspense boundaries;
- start independent data work in parallel where possible;
- decide status/headers before piping;
- log server-render errors;
- maintain a shell-level failure response;
- define abort/disconnect policy;
- let build tooling own generated asset URLs;
- measure server latency, TTFB, LCP, and hydration cost separately.
Exerciseโ
Design an SSR dashboard with an immediate navigation shell, independently streaming profile/chart/activity regions, one failing region, and a ten-second abort budget.
Explain which output can be sent first, which work can run in parallel, and which failures can still change the HTTP response.
Interview questionsโ
Junior: What does streaming SSR improve compared with returning one final HTML string?
Mid-level: What is the difference between onShellReady and onAllReady?
Senior: How do Suspense placement, HTTP status timing, server waterfalls, abort policy, and hydration interact in a production streaming route?