Course contentsShow
← React
Lesson 1635 of 2,39640. Server-Side RenderingFree lesson

How Streaming SSR Works

Understanding how renderToPipeableStream pipes HTML fragments progressively to the HTTP response stream.

How Streaming SSR Works

What you'll learn: How React progressively sends HTML fragments to the browser instead of waiting for the entire page to render.

The Core Idea

When you use renderToPipeableStream (which we introduced in the previous lesson), React doesn't generate one giant HTML string and send it all at once. Instead, it streams HTML fragments as they become ready, sending pieces of your page to the browser incrementally.

Think of it like a restaurant: Instead of waiting for the entire multi-course meal to be prepared before serving anything (the renderToString approach), the kitchen sends out each course as soon as it's ready. Your guests start eating the appetizer while the main course is still cooking.

How the Piping Works

The renderToPipeableStream API creates a readable stream that connects directly to the HTTP response stream:

const { pipe } = renderToPipeableStream(<App />, {
  onShellReady() {
    response.setHeader('Content-Type', 'text/html');
    pipe(response);
  }
});

Here's what happens:

  1. React starts rendering your component tree
  2. As each component completes, React writes its HTML to the pipe
  3. That HTML flows directly into the HTTP response
  4. The browser receives and displays partial content immediately
  5. More HTML arrives and gets appended as React continues rendering

The Browser Experience

The browser doesn't wait for the </html> closing tag. It receives chunks like:

<!DOCTYPE html><html><head>...</head><body><header>...</header>
<!-- Browser displays this immediately -->
<main>...
<!-- More content arrives progressively -->
</main></body></html>

Key Takeaway: Streaming SSR pipes HTML fragments progressively through renderToPipeableStream, letting browsers display content before the entire page finishes rendering on the server.