Skip to content

Commit ab24f64

Browse files
authored
[Fizz] use microtasks rather than tasks when scheduling work while prerendering (#30770)
Similar to #30768 we want to schedule work during prerendering in microtasks both for the root task and pings. We continue to schedule flushes as Tasks to allow as much work to be batched up as possible.
1 parent 1228a28 commit ab24f64

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

packages/react-server/src/ReactFizzServer.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import {describeObjectForErrorMessage} from 'shared/ReactSerializationErrors';
3838

3939
import {
4040
scheduleWork,
41+
scheduleMicrotask,
4142
beginWriting,
4243
writeChunk,
4344
writeChunkAndReturn,
@@ -669,7 +670,11 @@ function pingTask(request: Request, task: Task): void {
669670
pingedTasks.push(task);
670671
if (request.pingedTasks.length === 1) {
671672
request.flushScheduled = request.destination !== null;
672-
scheduleWork(() => performWork(request));
673+
if (request.trackedPostpones !== null) {
674+
scheduleMicrotask(() => performWork(request));
675+
} else {
676+
scheduleWork(() => performWork(request));
677+
}
673678
}
674679
}
675680

@@ -4893,12 +4898,22 @@ function flushCompletedQueues(
48934898

48944899
export function startWork(request: Request): void {
48954900
request.flushScheduled = request.destination !== null;
4896-
if (supportsRequestStorage) {
4897-
scheduleWork(() => requestStorage.run(request, performWork, request));
4901+
if (request.trackedPostpones !== null) {
4902+
// When prerendering we use microtasks for pinging work
4903+
if (supportsRequestStorage) {
4904+
scheduleMicrotask(() =>
4905+
requestStorage.run(request, performWork, request),
4906+
);
4907+
} else {
4908+
scheduleMicrotask(() => performWork(request));
4909+
}
48984910
} else {
4899-
scheduleWork(() => performWork(request));
4900-
}
4901-
if (request.trackedPostpones === null) {
4911+
// When rendering/resuming we use regular tasks and we also emit early preloads
4912+
if (supportsRequestStorage) {
4913+
scheduleWork(() => requestStorage.run(request, performWork, request));
4914+
} else {
4915+
scheduleWork(() => performWork(request));
4916+
}
49024917
// this is either a regular render or a resume. For regular render we want
49034918
// to call emitEarlyPreloads after the first performWork because we want
49044919
// are responding to a live request and need to balance sending something early

0 commit comments

Comments
 (0)