@samitouri / QOS-React / commits / 98773466ce

[Fizz] Don't outline Boundaries that may contribute to the preamble (#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

Josh Story committed Jul 30, 2025 at 18:06 UTC 98773466ce6736a5ffb7e54c1f4b44645ac18a80
2 files changed +43 -1
packages/react-dom/src/__tests__/ReactDOMFizzServer-test.js
+36
@@ -10695,4 +10695,40 @@ describe('ReactDOMFizzServer', () => {
10695
10696 expect(getVisibleChildren(container)).toEqual(<div>Success!</div>);
10697 });
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 + });
10734 });
packages/react-server/src/ReactFizzServer.js
+7 -1
@@ -460,7 +460,13 @@ function isEligibleForOutlining(
460 // For very small boundaries, don't bother producing a fallback for outlining.
461 // The larger this limit is, the more we can save on preparing fallbacks in case we end up
462 // 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 + );
470 }
471
472 function defaultErrorHandler(error: mixed) {