← Back to Frontend, page 2

frontend-0026 · Hard senior

What is streaming server rendering, and how do selective hydration, islands and server components reduce the JavaScript sent to the browser?

Streaming sends the HTML in pieces: the shell first, and slow parts later as their data becomes ready. Selective hydration, islands and server components then limit how much JavaScript must run, by hydrating only the interactive parts, or by never sending code for parts that need no interactivity.

Full answer

Classic SSR waits for all data before it sends anything. One slow query delays the whole page and raises TTFB. Streaming uses chunked transfer encoding: the server writes the shell with the header and the main content at once, flushes it, and writes slower sections later when their data is ready. The browser parses and paints the first chunk while the rest is still coming. This helps TTFB and LCP when the main content is fast and a small section is slow.

Frameworks build on this in different ways:

  • Suspense boundaries mark the parts that may arrive late. The server sends a placeholder first, and later sends the real content with a small inline script or instruction that swaps it in.
  • Selective hydration lets the client hydrate parts as their code arrives, and in React it can prioritise the part the user clicked on.
  • Islands architecture renders the page as static HTML and hydrates only marked interactive islands such as a search box or a video player. Everything else needs no JavaScript.
  • Server components run only on the server. Their code and large dependencies, such as a Markdown parser, never go to the browser, and only the output is sent. Interactive client components are still hydrated.

Cost and risks:

  • Streaming makes the status code and headers fixed before the slow part runs, so an error later cannot change the status to 500. Handle errors in the stream.
  • Some proxies buffer responses and defeat streaming. Test the real deployment path.
  • Crawlers and users without JavaScript still get the HTML, which is useful.

The Node script writes the shell, waits 150 milliseconds, and writes the rest. A client reads the response as a stream and shows that the shell arrived before the slow part.

javascript
import http from 'node:http';

const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
const server = http.createServer(async (req, res) => {
  res.setHeader('content-type', 'text/html');
  res.write('<!doctype html><h1>Course</h1><p>Shell is ready</p>');
  await sleep(150);                       // a slow query
  res.end('<ul><li>Recommended lesson</li></ul>');
});
await new Promise((resolve) => server.listen(0, resolve));

const res = await fetch(`http://localhost:${server.address().port}/`);
const reader = res.body.getReader();
const decoder = new TextDecoder();
const start = performance.now();
const seen = [];
for (;;) {
  const { value, done } = await reader.read();
  if (done) break;
  seen.push({ at: performance.now() - start, text: decoder.decode(value) });
}
console.log('shell came first:', seen[0].text.includes('Shell is ready'));
console.log('slow part came later:', seen.at(-1).text.includes('Recommended') && seen.at(-1).at - seen[0].at > 100);
server.close();

Run with Node 24.22.1. The output matched the text in the answer in two runs. Run with Node and the output was identical in two runs. The timing check uses a wide margin. A framework with Suspense, a proxy or a CDN was not used.

Key points

  • Streaming flushes the shell first and sends slow parts when ready.
  • It helps TTFB and LCP when only a small part of the page is slow.
  • Selective hydration and islands limit the code that runs in the browser.
  • Server components keep code and dependencies off the client.
  • After streaming starts the status code cannot change.

Common mistakes

  • Assuming streaming makes the total work smaller. It only changes when the user sees the parts.
  • Forgetting that a proxy can buffer the response.
  • Putting the LCP element behind a slow Suspense boundary.
  • Treating islands and server components as the same thing.

Follow-ups

  • How do you handle an error that happens after the shell was sent?
  • What does a client component boundary mean in a server component tree?
  • Which metric can get worse with streaming?

Tips

  • Explain the mechanism first (chunks), then the framework names.
  • Name one risk, such as buffering or status codes, to show depth.