From c8a035036d0f257c514b3628e927dd9dd26e5a09 Mon Sep 17 00:00:00 2001
From: Josh Story
Date: Thu, 11 Apr 2024 15:13:04 -0700
Subject: [PATCH] [Fizz] hoistables should never flush before the preamble
(#28802)
Hoistables should never flush before the preamble however there is a
surprisingly easy way to trigger this to happen by suspending in the
shell of the app. This change modifies the flushing behavior to not emit
any hoistables before the preamble has written. It accomplishes this by
aborting the flush early if there are any pending root tasks remaining.
It's unfortunate we need this extra condition but it's essential that we
don't emit anything before the preamble and at the moment I don't see a
way to do that without introducing a new condition.
There is a test that began to fail with this update. It turns out that
in node the root can be blocked during a resume even for a component
inside a Suspense boundary if that boundary was part of the prerender.
This means that with the current heuristic in this PR boundaries cannot
be flushed during resume until the root is unblocked. This is not ideal
but this is already how Edge works because the root blocks the stream in
that case. This just makes Node deopt in a similar way to edge. We
should improve this but we ought to do so in a way that works for edge
too and it needs to be more comprehensive.
---
.../src/__tests__/ReactDOMFizzServer-test.js | 9 +++-
.../src/__tests__/ReactDOMFloat-test.js | 45 +++++++++++++++++++
packages/react-server/src/ReactFizzServer.js | 19 ++++----
3 files changed, 64 insertions(+), 9 deletions(-)
diff --git a/packages/react-dom/src/__tests__/ReactDOMFizzServer-test.js b/packages/react-dom/src/__tests__/ReactDOMFizzServer-test.js
index df1c71f3fd586..50afd73d1f937 100644
--- a/packages/react-dom/src/__tests__/ReactDOMFizzServer-test.js
+++ b/packages/react-dom/src/__tests__/ReactDOMFizzServer-test.js
@@ -6926,7 +6926,14 @@ describe('ReactDOMFizzServer', () => {
expect(getVisibleChildren(container)).toEqual(
{'Loading1'}
- {'Hello'}
+ {/*
+ This used to show "Hello" in this slot because the boundary was able to be flushed
+ early but we now prevent flushing while pendingRootTasks is not zero. This is how Edge
+ would work anyway because you don't get the stream until the root is unblocked on a resume
+ so Node now aligns with edge bevavior
+ {'Hello'}
+ */}
+ {'Loading2'}
{'Loading3'}
,
);
diff --git a/packages/react-dom/src/__tests__/ReactDOMFloat-test.js b/packages/react-dom/src/__tests__/ReactDOMFloat-test.js
index 3eabcb5781c29..b2331c17f2829 100644
--- a/packages/react-dom/src/__tests__/ReactDOMFloat-test.js
+++ b/packages/react-dom/src/__tests__/ReactDOMFloat-test.js
@@ -5034,6 +5034,51 @@ body {
);
});
+ it('should never flush hoistables before the preamble', async () => {
+ let resolve;
+ const promise = new Promise(res => {
+ resolve = res;
+ });
+
+ function App() {
+ ReactDOM.preinit('foo', {as: 'script'});
+ React.use(promise);
+ return (
+
+ hello
+
+ );
+ }
+
+ await act(() => {
+ renderToPipeableStream().pipe(writable);
+ });
+
+ // we assert the default JSDOM still in tact
+ expect(getMeaningfulChildren(document)).toEqual(
+
+
+
+
+
+ ,
+ );
+
+ await act(() => {
+ resolve();
+ });
+
+ // we assert the DOM was replaced entirely because we streamed an opening html tag
+ expect(getMeaningfulChildren(document)).toEqual(
+
+
+
+
+ hello
+ ,
+ );
+ });
+
describe('ReactDOM.prefetchDNS(href)', () => {
it('creates a dns-prefetch resource when called', async () => {
function App({url}) {
diff --git a/packages/react-server/src/ReactFizzServer.js b/packages/react-server/src/ReactFizzServer.js
index 223d63a4c1084..7ba6f334e759a 100644
--- a/packages/react-server/src/ReactFizzServer.js
+++ b/packages/react-server/src/ReactFizzServer.js
@@ -4057,22 +4057,25 @@ function flushCompletedQueues(
// that item fully and then yield. At that point we remove the already completed
// items up until the point we completed them.
+ if (request.pendingRootTasks > 0) {
+ // When there are pending root tasks we don't want to flush anything
+ return;
+ }
+
let i;
const completedRootSegment = request.completedRootSegment;
if (completedRootSegment !== null) {
if (completedRootSegment.status === POSTPONED) {
// We postponed the root, so we write nothing.
return;
- } else if (request.pendingRootTasks === 0) {
- flushPreamble(request, destination, completedRootSegment);
- flushSegment(request, destination, completedRootSegment, null);
- request.completedRootSegment = null;
- writeCompletedRoot(destination, request.renderState);
- } else {
- // We haven't flushed the root yet so we don't need to check any other branches further down
- return;
}
+
+ flushPreamble(request, destination, completedRootSegment);
+ flushSegment(request, destination, completedRootSegment, null);
+ request.completedRootSegment = null;
+ writeCompletedRoot(destination, request.renderState);
}
+
writeHoistables(destination, request.resumableState, request.renderState);
// We emit client rendering instructions for already emitted boundaries first.
// This is so that we can signal to the client to start client rendering them as