FormidableLabs/react-ssr-prepass

Throws "ReferenceError: setImmediate is not defined" in browser

zenflow opened this issue · 2 comments

The setImmediate function (used in this package) is not present on the majority of browsers. (reference) We should replace it with something more standard and make this package more portable. The first idea (for a replacement) that comes to mind is Promise.resolve().then since I think it's the most universal/portable way.

You might wonder why I am trying to use this on the browser. (And I guess I am evidently the first to try it.) The reason is because I want to prefetch data for some pages (in my Next.js apps) before rendering/showing them, regardless of whether the rendering is happening server-side or browser-side, to get rid of unwanted <Loader/> renders where I have nothing to show the user. Instead the user waits on the previous page until the next page is ready. The user can navigate from fully-formed page to fully-formed page, within the site now, not just when entering the site (when SSR is used). It seems like generally a better UX.

I’ll have another look 👍 sounds like an interesting use-case that I simply haven’t considered before!

Unfortunately this won’t be as simple as swapping setImmediate out with a Promise.resolve() tick.

These two are subtly different in Node. While setImmediate will give the event loop micro-ticks time to flush and complete, Promise.resolve() timings, while also being on the micro-tick queue, may not wait and be processed too early.

This defeats the presence of this code, which is intended to stop the event loop from being overwhelmed on the server-side.

It’s probably more fitting to add some code to disable the event loop yielding entirely in the browser :)

It’s probably more fitting to add some code to disable the event loop yielding entirely in the browser :)

@kitten Something like PR #50?