Post

A new React 19 feature that might have gone unnoticed: ReactDOMStatic.prerender(<App />) It's like the old renderToString API, but unlike renderToString, prerender will wait for Suspense boundaries to finish before returning the HTML.

15 reposts192 likes

I mean yeah but they’re not server components they just dehydrated client components same as before with renderToString except you can suspend. If you try to hydrate that you’ll hit issues with the async/await and hooks

prerender is a promise, the response is a stream because it supports more modern use cases bsky.app/profile/rick...

Ricky@ricky.fm

Even for SSG, if you’re uploading to a CDN it’s more efficient to use their streaming APIs and skip decoding and encoding again, but in the future prerender could be used to stream the shell in a response and resume the shell for dynamic content.

I’ll admit I had the same question at first, but when you consider the possible use cases a stream makes sense

Possibly silly question: what's the difference between this vs onAllReady via the standard react streaming APIs? I'm assuming the latter is still the recommended way to fully buffer pages for bots, for example? Curious if there are limitations in one or the other I'm overlooking

The former with onAllReady will emit all the intermediate suspense fallbacks plus the content, this will just contain the html with content, so it’s more optimized for this use case.

Ah, thanks - that's interesting since the bot usecase in my mind seems like it's actually closer to wanting just the html (since presumably intermediate fallbacks require js to patch up?), which would seem to favour prerender over onAllReady This is assuming we want zero-js-required first paint

Maybe "require js to patch up" isn't quite right since I'd imagine they end up as hidden elements in the DOM anyway, so bots wouldn't care. I want to spend some more time with these APIs - this one seems very handy!

the difference is that it only outputs the content instead of emitting a stream with the intermediate fallback states and then the content

That's the first example in the usage section isn't it? There's an example for using the stream and for rendering the stream to a string?

Even for SSG, if you’re uploading to a CDN it’s more efficient to use their streaming APIs and skip decoding and encoding again, but in the future prerender could be used to stream the shell in a response and resume the shell for dynamic content.

You can always read the stream to get a string, but you can’t convert a string back to a stream cost free.