GoogleChromeLabs / prerender-loader

📰 Painless universal pre-rendering for Webpack.
https://npm.im/prerender-loader
Apache License 2.0
1.9k stars 50 forks source link

React "renderToNodeStream" to increase speed? #19

Closed murshex closed 6 years ago

murshex commented 6 years ago

Is it possible to use "renderToNodeStream" to increase the build time?

hugmanrique commented 6 years ago

This project is framework agnostic, so it doesn't know (and assume) anything about your site's content. Even if this loader was oriented towards React users, using the renderToNodeStream function wouldn't solve anything as this loader and JSDOM would have to wait for the entire stream to be generated (streams are useful for directly piping React's output to a HTTP socket).

developit commented 6 years ago

Correct - since this is prerendering, the output is a static content so there's no value in streaming VS buffered rendering.

FWIW your prerender can return a Promise, so you could invoke pipe the result of renderToNodeStream() into a Buffer and wrap that in a promise. I can never remember these APIs without looking, but it'd be something like:

import getStream from 'get-stream'  // see https://npm.im/get-stream

export default function() {
  const stream = renderToNodeStream(<App />)
  // collect the streamed HTML up into a string and return a promise that resolves when done:
  return getStream(stream)
}

This is all likely a bit slower than just using renderToString(), since there is very little difference between that and the stream renderer. Both render synchronous, the streaming renderer just serializes lazily.