Q37: What is the difference between renderToString and renderToPipeableStream?

  • renderToString: Legacy synchronous SSR method that generates a complete HTML string. Blocks server response until the entire component tree finishes rendering.
  • renderToPipeableStream (Node.js) / renderToReadableStream (Edge): Modern streaming SSR method that streams HTML chunks progressively to the client as components resolve.
Streaming SSR with renderToPipeableStream
import { renderToPipeableStream } from 'react-dom/server';

app.get('/', (req, res) => {
  const stream = renderToPipeableStream(<App />,
    {
      bootstrapScripts: ['/main.js'],
      onShellReady() {
        res.statusCode = 200;
        res.setHeader('content-type', 'text/html');
        stream.pipe(res); // Stream HTML shell immediately!
      }
    }
  );
});

Streaming SSR sends the HTML shell immediately, streaming suspended content as asynchronous promises resolve.

Key Takeaways
  • renderToString is synchronous and blocks HTTP server responses.

  • renderToPipeableStream streams HTML chunks and enables Selective Hydration with Suspense.

Knowledge Check

What performance advantage does renderToPipeableStream provide over legacy renderToString?