Use setTimeout to schedule work on the server in Edge environments (#26348)

We rely heavily on being able to batch rendering after multiple fetches
etc. have completed on the server. However, we only do this in the
Node.js build. Node.js `setImmediate` has the exact semantics we need.
To be after the current cycle of I/O so that we can collect after all
those I/O events already in the queue has been processed.

This doesn't exist in standard browsers, so we ended up not using it
there. We could've used `setTimeout` but that risks being throttled
which would severely negatively affect the performance so we just did it
synchronously there. We probably could just use the `scheduler` there.

Now we have a separate build for Edge where `setTimeout(..., 0)`
actually behaves like `setImmediate` which is what we want. So we can
just use that in that build.

@Jarred-Sumner not sure what you want for Bun.
This commit is contained in:
Sebastian Markbåge
2023-03-08 15:34:29 -05:00
committed by GitHub
parent 83643778bd
commit d8e49f2af8
3 changed files with 9 additions and 1 deletions
@@ -15,6 +15,10 @@ global.ReadableStream =
global.TextEncoder = require('util').TextEncoder;
global.TextDecoder = require('util').TextDecoder;
// Don't wait before processing work on the server.
// TODO: we can replace this with FlightServer.act().
global.setTimeout = cb => cb();
let clientExports;
let webpackMap;
let webpackModules;
+1 -1
View File
@@ -13,7 +13,7 @@ export type PrecomputedChunk = Uint8Array;
export opaque type Chunk = Uint8Array;
export function scheduleWork(callback: () => void) {
callback();
setTimeout(callback, 0);
}
export function flushBuffered(destination: Destination) {
@@ -20,6 +20,10 @@ global.Response = require('node-fetch').Response;
// Patch for Edge environments for global scope
global.AsyncLocalStorage = require('async_hooks').AsyncLocalStorage;
// Don't wait before processing work on the server.
// TODO: we can replace this with FlightServer.act().
global.setTimeout = cb => cb();
let fetchCount = 0;
async function fetchMock(resource, options) {
fetchCount++;