Streaming SSR in Rust, without the headache
How we built a streaming SSR pipeline that handles async components and Suspense correctly.
Streaming SSR is one of those features that sounds easy and is, in fact, not. The list of things that can go wrong is long: async components that need to suspend, Suspense boundaries that need to flush, hydration mismatches when the server and client disagree about what to render.
We started from React's renderToReadableStream and built on top of it. The result is a pipeline that:
1. Starts sending HTML within a few milliseconds of the request landing. 2. Streams async segments as they resolve, with no buffer in between. 3. Hydrates the page progressively, so the user can interact with above-the-fold content immediately.
The hardest part was getting the error boundary semantics right. We use Rust-style error codes (SR-E0001, SR-E0401, etc.) that are emitted at compile time by our SWC plugin, and a sleek dark error overlay that shows up in the dev server.