Skip to content

Commit 9877346

Browse files
authored
[Fizz] Don't outline Boundaries that may contribute to the preamble (facebook#34058)
Suspense boundaries that may have contributed to the preamble should not be outlined due to size because these boundaries are only meant to be in fallback state if the boundary actually errors. This change excludes any boundary which has the potential to contribute to the preamble. We could alternatively track which boundaries actually contributed to the preamble but in practice there will be very few and I think this is sufficient. One problem with this approach is it makes Suspense above body opt out of the mode where we omit rel="expect" for large shells. In essence Suspense above body has the semantics of a Shell (it blocks flushing until resolved) but it doesn't get tracked as request bytes and thus we will not opt users into the skipped blocking shell for very large boundaries. This will be fixed in a followup
1 parent 9784cb3 commit 9877346

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10695,4 +10695,40 @@ describe('ReactDOMFizzServer', () => {
1069510695

1069610696
expect(getVisibleChildren(container)).toEqual(<div>Success!</div>);
1069710697
});
10698+
10699+
it('should always flush the boundaries contributing the preamble regardless of their size', async () => {
10700+
const longDescription =
10701+
`I need to make this segment somewhat large because it needs to be large enought to be outlined during the initial flush. Setting the progressive chunk size to near zero isn't enough because there is a fixed minimum size that we use to avoid doing the size tracking altogether and this needs to be larger than that at least.
10702+
10703+
Unfortunately that previous paragraph wasn't quite long enough so I'll continue with some more prose and maybe throw on some repeated additional strings at the end for good measure.
10704+
10705+
` + 'a'.repeat(500);
10706+
10707+
const randomTag = Math.random().toString(36).slice(2, 10);
10708+
10709+
function App() {
10710+
return (
10711+
<Suspense fallback={randomTag}>
10712+
<html lang="en">
10713+
<body>
10714+
<main>{longDescription}</main>
10715+
</body>
10716+
</html>
10717+
</Suspense>
10718+
);
10719+
}
10720+
10721+
let streamedContent = '';
10722+
writable.on('data', chunk => (streamedContent += chunk));
10723+
10724+
await act(() => {
10725+
renderToPipeableStream(<App />, {progressiveChunkSize: 100}).pipe(
10726+
writable,
10727+
);
10728+
});
10729+
10730+
// We don't use the DOM here b/c we execute scripts which hides whether a fallback was shown briefly
10731+
// Instead we assert that we never emitted the fallback of the Suspense boundary around the body.
10732+
expect(streamedContent).not.toContain(randomTag);
10733+
});
1069810734
});

packages/react-server/src/ReactFizzServer.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,13 @@ function isEligibleForOutlining(
460460
// For very small boundaries, don't bother producing a fallback for outlining.
461461
// The larger this limit is, the more we can save on preparing fallbacks in case we end up
462462
// outlining.
463-
return boundary.byteSize > 500;
463+
return (
464+
boundary.byteSize > 500 &&
465+
// For boundaries that can possibly contribute to the preamble we don't want to outline
466+
// them regardless of their size since the fallbacks should only be emitted if we've
467+
// errored the boundary.
468+
boundary.contentPreamble === null
469+
);
464470
}
465471

466472
function defaultErrorHandler(error: mixed) {

0 commit comments

Comments
 (0)