Skip to content

[pull] main from facebook:main #205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions packages/react-dom/src/__tests__/ReactDOMFizzServer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10695,4 +10695,93 @@ describe('ReactDOMFizzServer', () => {

expect(getVisibleChildren(container)).toEqual(<div>Success!</div>);
});

it('should always flush the boundaries contributing the preamble regardless of their size', async () => {
const longDescription =
`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.

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.

` + 'a'.repeat(500);

const randomTag = Math.random().toString(36).slice(2, 10);

function App() {
return (
<Suspense fallback={randomTag}>
<html lang="en">
<body>
<main>{longDescription}</main>
</body>
</html>
</Suspense>
);
}

let streamedContent = '';
writable.on('data', chunk => (streamedContent += chunk));

await act(() => {
renderToPipeableStream(<App />, {progressiveChunkSize: 100}).pipe(
writable,
);
});

// We don't use the DOM here b/c we execute scripts which hides whether a fallback was shown briefly
// Instead we assert that we never emitted the fallback of the Suspense boundary around the body.
expect(streamedContent).not.toContain(randomTag);
});

it('should track byte size of shells that may contribute to the preamble when determining if the blocking render exceeds the max size', async () => {
const longDescription =
`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.

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.

` + 'a'.repeat(500);

const randomTag = Math.random().toString(36).slice(2, 10);

function App() {
return (
<>
<Suspense fallback={randomTag}>
<html lang="en">
<body>
<main>{longDescription}</main>
</body>
</html>
</Suspense>
<div>Outside Preamble</div>
</>
);
}

let streamedContent = '';
writable.on('data', chunk => (streamedContent += chunk));

const errors = [];
await act(() => {
renderToPipeableStream(<App />, {
progressiveChunkSize: 5,
onError(e) {
errors.push(e);
},
}).pipe(writable);
});

if (gate(flags => flags.enableFizzBlockingRender)) {
expect(errors.length).toBe(1);
expect(errors[0].message).toContain(
// We set the chunk size low enough that the threshold rounds to zero kB
'This rendered a large document (>0 kB) without any Suspense boundaries around most of it.',
);
} else {
expect(errors.length).toBe(0);
}

// We don't use the DOM here b/c we execute scripts which hides whether a fallback was shown briefly
// Instead we assert that we never emitted the fallback of the Suspense boundary around the body.
expect(streamedContent).not.toContain(randomTag);
});
});
16 changes: 15 additions & 1 deletion packages/react-server/src/ReactFizzServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,13 @@ function isEligibleForOutlining(
// For very small boundaries, don't bother producing a fallback for outlining.
// The larger this limit is, the more we can save on preparing fallbacks in case we end up
// outlining.
return boundary.byteSize > 500;
return (
boundary.byteSize > 500 &&
// For boundaries that can possibly contribute to the preamble we don't want to outline
// them regardless of their size since the fallbacks should only be emitted if we've
// errored the boundary.
boundary.contentPreamble === null
);
}

function defaultErrorHandler(error: mixed) {
Expand Down Expand Up @@ -5498,6 +5504,9 @@ function preparePreambleFromSegment(
// This boundary is complete. It might have inner boundaries which are pending
// and able to provide a preamble so we have to check it's children
hoistPreambleState(request.renderState, preamble);
// We track this boundary's byteSize on the request since it will always flush with
// the request since it may contribute to the preamble
request.byteSize += boundary.byteSize;
const boundaryRootSegment = boundary.completedSegments[0];
if (!boundaryRootSegment) {
// Using the same error from flushSegment to avoid making a new one since conceptually the problem is still the same
Expand Down Expand Up @@ -5544,13 +5553,18 @@ function preparePreamble(request: Request) {
request.completedPreambleSegments === null
) {
const collectedPreambleSegments: Array<Array<Segment>> = [];
const originalRequestByteSize = request.byteSize;
const hasPendingPreambles = preparePreambleFromSegment(
request,
request.completedRootSegment,
collectedPreambleSegments,
);
if (isPreambleReady(request.renderState, hasPendingPreambles)) {
request.completedPreambleSegments = collectedPreambleSegments;
} else {
// We restore the original size since the preamble is not ready
// and we will prepare it again.
request.byteSize = originalRequestByteSize;
}
}
}
Expand Down
Loading