ReactMedium

What is Streaming?

Streaming server rendering sends HTML to the browser in chunks as each part of the page becomes ready, instead of waiting for all data before sending anything. In React, <Suspense> boundaries define the chunks: the server sends the shell with fallbacks immediately, then streams each boundary's content (plus a small inline script to swap it in) when its data resolves.

How React streams

  • renderToPipeableStream (Node.js) and renderToReadableStream (Web streams; also available in Node since React 19.2) render the shell and keep the connection open.
  • Suspended components show their fallback first; when resolved, React streams the HTML and moves it into place.
  • Selective hydration: each chunk can hydrate independently, with priority for parts the user interacts with.
  • React 19.2 batches Suspense reveals during SSR so content appears in fewer, smoother steps.

Benefits

  • Lower time to first byte and first contentful paint.
  • Slow data (recommendations, comments) does not block fast data (product title).
  • Better perceived performance with progressive loading.

Design tips

  • Put boundaries around independent slow sections.
  • Start data fetching early and in parallel so streaming has something to stream.
  • Send critical content in the shell (title, main image).

Caveats

  • Status codes and headers are fixed once streaming starts; decide on 404s or redirects before the first flush.
  • Some proxies buffer responses and break streaming.
  • Bots that need complete HTML can be served a fully rendered response (onAllReady instead of onShellReady).

Frameworks

Next.js (loading.tsx, <Suspense>), React Router (deferred data with <Await> or promises), TanStack Start.

Code example

tsx
1// server.ts - streaming with Suspense
2import { renderToPipeableStream } from "react-dom/server";
3import { Suspense, use } from "react";
4import type { ServerResponse } from "node:http";
5
6const slow = <T,>(value: T, ms: number) => new Promise<T>((r) => setTimeout(() => r(value), ms));
7
8function Reviews({ reviews }: { reviews: Promise<string[]> }) {
9 const list = use(reviews); // suspends until resolved; streamed when ready
10 return <ul>{list.map((r) => <li key={r}>{r}</li>)}</ul>;
11}
12
13function ProductPage() {
14 const reviews = slow(["Great!", "Fast shipping"], 2000);
15 return (
16 <html lang="en">
17 <body>
18 <h1>Running shoe</h1> {/* in the first chunk */}
19 <Suspense fallback={<p>Loading reviews…</p>}>
20 <Reviews reviews={reviews} /> {/* streamed ~2s later */}
21 </Suspense>
22 </body>
23 </html>
24 );
25}
26
27export function handle(res: ServerResponse, isBot: boolean) {
28 const stream = renderToPipeableStream(<ProductPage />, {
29 onShellReady() { if (!isBot) { res.setHeader("Content-Type", "text/html"); stream.pipe(res); } },
30 onAllReady() { if (isBot) { res.setHeader("Content-Type", "text/html"); stream.pipe(res); } }, // full HTML for crawlers
31 });
32}

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the React set.

Open in study app