Sub topic 3
Streaming HTML with `renderToPipeableStream`
Assume you know function components, hooks, JSX, basic Node.js, and browser DOM behavior. Streaming server-side rendering (SSR) sends HTML as it becomes ready instead of waiting for the entire React tree.
Stream the shell and handle server failures
renderToPipeableStream returns a readable React stream. The shell is the page structure outside suspended content. Start sending it in onShellReady. Abort slow renders so a request cannot remain open forever.
import express from 'express';
import { renderToPipeableStream } from 'react-dom/server';
import App from './App.js';
const app = express();
app.get('/', (req, res) => {
let didError = false;
const { pipe, abort } = renderToPipeableStream(<App />, {
bootstrapScripts: ['/static/client.js'],
onShellReady() {
res.statusCode = didError ? 500 : 200;
res.setHeader('Content-Type', 'text/html');
pipe(res);
},
onShellError(error) {
console.error(error);
res.statusCode = 500;
res.send('<h1>Something went wrong</h1>');
},
onError(error) {
didError = true;
console.error(error);
}
});
setTimeout(abort, 10000);
});
app.listen(3000);onShellError runs before any response is sent. onError can report an error after streaming has started, when changing the status code may no longer be possible.
Suspense and selective hydration
A Suspense boundary provides a fallback while its content is unavailable. React can stream the shell and fallback first, then insert the completed boundary later.
import { lazy, Suspense } from 'react';
const Comments = lazy(() => import('./Comments.js'));
export default function Page() {
return (
<main>
<h1>Article</h1>
<Suspense fallback={<p>Loading comments...</p>}>
<Comments />
</Suspense>
</main>
);
}Selective hydration lets React hydrate boundaries independently. An interactive boundary can hydrate when its code and data are ready, or receive priority when the user interacts with it. The whole page does not need to wait.
Recoverable errors and debugging
React may recover from a hydration mismatch by rendering part of the tree on the client. Capture these events with onRecoverableError.
hydrateRoot(document, <App />, {
onRecoverableError(error, errorInfo) {
console.error(error, errorInfo.componentStack);
}
});Debug in this order:
- Read the first hydration error and its component stack.
- Compare server and first-client-render output.
- Check time, randomness, browser-only APIs, and invalid HTML nesting.
- Move client-only values into
useEffect, useuseIdfor generated IDs, and provide server snapshots for external stores. - Test the smallest failing component.
supressHydrationWarning is only a narrow escape hatch for intentionally different text. It does not repair the underlying tree.
Server Components and SSR
Server Components and SSR solve different problems. SSR sends rendered HTML for the initial view. Server Components decide which components execute on the server and can reduce client JavaScript. A framework may use both: Server Components produce server-side content, SSR sends the initial HTML, and hydration activates client components.
Exercise
Why should a streaming server abort a render after a timeout?
Show answer
To prevent slow data or a failed render from keeping the request open indefinitely.