Skip to content

[Fizz] Unblock SuspenseList when prerendering #33321

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
May 21, 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
83 changes: 83 additions & 0 deletions packages/react-dom/src/__tests__/ReactDOMFizzStaticBrowser-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ let ReactDOM;
let ReactDOMFizzServer;
let ReactDOMFizzStatic;
let Suspense;
let SuspenseList;
let container;
let Scheduler;
let act;
Expand All @@ -50,6 +51,7 @@ describe('ReactDOMFizzStaticBrowser', () => {
ReactDOMFizzServer = require('react-dom/server.browser');
ReactDOMFizzStatic = require('react-dom/static.browser');
Suspense = React.Suspense;
SuspenseList = React.unstable_SuspenseList;
container = document.createElement('div');
document.body.appendChild(container);
});
Expand Down Expand Up @@ -2242,4 +2244,85 @@ describe('ReactDOMFizzStaticBrowser', () => {
</html>,
);
});

// @gate enableHalt && enableSuspenseList
it('can resume a partially prerendered SuspenseList', async () => {
const errors = [];

let resolveA;
const promiseA = new Promise(r => (resolveA = r));
let resolveB;
const promiseB = new Promise(r => (resolveB = r));

async function ComponentA() {
await promiseA;
return 'A';
}

async function ComponentB() {
await promiseB;
return 'B';
}

function App() {
return (
<div>
<SuspenseList revealOrder="forwards">
<Suspense fallback="Loading A">
<ComponentA />
</Suspense>
<Suspense fallback="Loading B">
<ComponentB />
</Suspense>
<Suspense fallback="Loading C">C</Suspense>
</SuspenseList>
</div>
);
}

const controller = new AbortController();
const pendingResult = serverAct(() =>
ReactDOMFizzStatic.prerender(<App />, {
signal: controller.signal,
onError(x) {
errors.push(x.message);
},
}),
);

await serverAct(() => {
controller.abort();
});

const prerendered = await pendingResult;
const postponedState = JSON.stringify(prerendered.postponed);

await readIntoContainer(prerendered.prelude);
expect(getVisibleChildren(container)).toEqual(
<div>
{'Loading A'}
{'Loading B'}
{'C' /* TODO: This should not be resolved. */}
</div>,
);

expect(prerendered.postponed).not.toBe(null);

await resolveA();
await resolveB();

const dynamic = await serverAct(() =>
ReactDOMFizzServer.resume(<App />, JSON.parse(postponedState)),
);

await readIntoContainer(dynamic);

expect(getVisibleChildren(container)).toEqual(
<div>
{'A'}
{'B'}
{'C'}
</div>,
);
});
});
7 changes: 7 additions & 0 deletions packages/react-server/src/ReactFizzServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4938,6 +4938,13 @@ function finishedTask(
// preparation work during the work phase rather than the when flushing.
preparePreamble(request);
}
} else if (boundary.status === POSTPONED) {
const boundaryRow = boundary.row;
if (boundaryRow !== null) {
if (--boundaryRow.pendingTasks === 0) {
finishSuspenseListRow(request, boundaryRow);
}
}
}
} else {
if (segment !== null && segment.parentFlushed) {
Expand Down
Loading