Skip to content

Commit 463b808

Browse files
authored
[Fizz] Reset the segent id assignment when postponing the root (facebook#33755)
When postponing the root we encode the segment Id into the postponed state but we should really be reseting it to zero so we can restart the counter from the beginning when the resume is actually just a re-render. This also no longer assigns the root segment id based on the postponed state when resuming the root for the same reason. In the future we may use the embedded replay segment id if we implement resuming the root without re-rendering everything but that is not yet implemented or planned.
1 parent 96c61b7 commit 463b808

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

packages/react-dom/src/__tests__/ReactDOMFizzStaticBrowser-test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1623,7 +1623,7 @@ describe('ReactDOMFizzStaticBrowser', () => {
16231623

16241624
expect(result).toBe(
16251625
'<!DOCTYPE html><html><head><link rel="expect" href="#_R_" blocking="render"/></head>' +
1626-
'<body>hello<!--$?--><template id="B:1"></template><!--/$--><script id="_R_">requestAnimationFrame(function(){$RT=performance.now()});</script>',
1626+
'<body>hello<!--$?--><template id="B:0"></template><!--/$--><script id="_R_">requestAnimationFrame(function(){$RT=performance.now()});</script>',
16271627
);
16281628

16291629
await 1;
@@ -1648,8 +1648,8 @@ describe('ReactDOMFizzStaticBrowser', () => {
16481648

16491649
expect(slice).toBe(
16501650
'<!DOCTYPE html><html><head><link rel="expect" href="#_R_" blocking="render"/></head>' +
1651-
'<body>hello<!--$?--><template id="B:1"></template><!--/$--><script id="_R_">requestAnimationFrame(function(){$RT=performance.now()});</script>' +
1652-
'<div hidden id="S:1">world<!-- --></div><script>$RX',
1651+
'<body>hello<!--$?--><template id="B:0"></template><!--/$--><script id="_R_">requestAnimationFrame(function(){$RT=performance.now()});</script>' +
1652+
'<div hidden id="S:0">world<!-- --></div><script>$RX',
16531653
);
16541654
});
16551655

packages/react-server/src/ReactFizzServer.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,6 @@ export function resumeRequest(
666666
request.nextSegmentId = postponedState.nextSegmentId;
667667

668668
if (typeof postponedState.replaySlots === 'number') {
669-
const resumedId = postponedState.replaySlots;
670669
// We have a resume slot at the very root. This is effectively just a full rerender.
671670
const rootSegment = createPendingSegment(
672671
request,
@@ -677,7 +676,6 @@ export function resumeRequest(
677676
false,
678677
false,
679678
);
680-
rootSegment.id = resumedId;
681679
// There is no parent so conceptually, we're unblocked to flush this segment.
682680
rootSegment.parentFlushed = true;
683681
const rootTask = createRenderTask(
@@ -6326,24 +6324,29 @@ export function getPostponedState(request: Request): null | PostponedState {
63266324
return null;
63276325
}
63286326
let replaySlots: ResumeSlots;
6327+
let nextSegmentId: number;
63296328
if (
63306329
request.completedRootSegment !== null &&
63316330
// The Root postponed
63326331
(request.completedRootSegment.status === POSTPONED ||
63336332
// Or the Preamble was not available
63346333
request.completedPreambleSegments === null)
63356334
) {
6336-
// This is necessary for the pending preamble case and is idempotent for the
6337-
// postponed root case
6338-
replaySlots = request.completedRootSegment.id;
6335+
nextSegmentId = 0;
6336+
// We need to ensure that on resume we retry the root. We use a number
6337+
// type for the replaySlots to signify this (see resumeRequest).
6338+
// The value -1 represents an unassigned ID but is not functionally meaningful
6339+
// for resuming at the root.
6340+
replaySlots = -1;
63396341
// We either postponed the root or we did not have a preamble to flush
63406342
resetResumableState(request.resumableState, request.renderState);
63416343
} else {
6344+
nextSegmentId = request.nextSegmentId;
63426345
replaySlots = trackedPostpones.rootSlots;
63436346
completeResumableState(request.resumableState);
63446347
}
63456348
return {
6346-
nextSegmentId: request.nextSegmentId,
6349+
nextSegmentId,
63476350
rootFormatContext: request.rootFormatContext,
63486351
progressiveChunkSize: request.progressiveChunkSize,
63496352
resumableState: request.resumableState,

0 commit comments

Comments
 (0)