Skip to content

[pull] main from facebook:main #96

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
132 changes: 131 additions & 1 deletion packages/react-dom/src/__tests__/ReactDOMFizzSuspenseList-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ let writable;
let container;
let buffer = '';
let hasErrored = false;
let hasCompleted = false;
let fatalError = undefined;

describe('ReactDOMFizSuspenseList', () => {
describe('ReactDOMFizzSuspenseList', () => {
beforeEach(() => {
jest.resetModules();
JSDOM = require('jsdom').JSDOM;
Expand Down Expand Up @@ -59,6 +60,7 @@ describe('ReactDOMFizSuspenseList', () => {

buffer = '';
hasErrored = false;
hasCompleted = false;

writable = new Stream.PassThrough();
writable.setEncoding('utf8');
Expand All @@ -69,6 +71,9 @@ describe('ReactDOMFizSuspenseList', () => {
hasErrored = true;
fatalError = error;
});
writable.on('finish', () => {
hasCompleted = true;
});
});

afterEach(() => {
Expand Down Expand Up @@ -103,7 +108,12 @@ describe('ReactDOMFizSuspenseList', () => {

function createAsyncText(text) {
let resolved = false;
let error = undefined;
const Component = function () {
if (error !== undefined) {
Scheduler.log('Error! [' + error.message + ']');
throw error;
}
if (!resolved) {
Scheduler.log('Suspend! [' + text + ']');
throw promise;
Expand All @@ -115,6 +125,10 @@ describe('ReactDOMFizSuspenseList', () => {
resolved = true;
return resolve();
};
Component.reject = function (e) {
error = e;
return resolve();
};
});
return Component;
}
Expand Down Expand Up @@ -714,4 +728,120 @@ describe('ReactDOMFizSuspenseList', () => {
</div>,
);
});

// @gate enableSuspenseList
it('can abort a pending SuspenseList', async () => {
const A = createAsyncText('A');

function Foo() {
return (
<div>
<SuspenseList revealOrder="forwards">
<Suspense fallback={<Text text="Loading A" />}>
<A />
</Suspense>
<Suspense fallback={<Text text="Loading B" />}>
<Text text="B" />
</Suspense>
</SuspenseList>
</div>
);
}

const errors = [];
let abortStream;
await serverAct(async () => {
const {pipe, abort} = ReactDOMFizzServer.renderToPipeableStream(<Foo />, {
onError(error) {
errors.push(error.message);
},
});
pipe(writable);
abortStream = abort;
});

assertLog([
'Suspend! [A]',
'B', // TODO: Defer rendering the content after fallback if previous suspended,
'Loading A',
'Loading B',
]);

expect(getVisibleChildren(container)).toEqual(
<div>
<span>Loading A</span>
<span>Loading B</span>
</div>,
);

await serverAct(() => {
abortStream();
});

expect(hasCompleted).toBe(true);
expect(errors).toEqual([
'The render was aborted by the server without a reason.',
]);
});

// @gate enableSuspenseList
it('can error a pending SuspenseList', async () => {
const A = createAsyncText('A');

function Foo() {
return (
<div>
<SuspenseList revealOrder="forwards">
<Suspense fallback={<Text text="Loading A" />}>
<A />
</Suspense>
<Suspense fallback={<Text text="Loading B" />}>
<Text text="B" />
</Suspense>
</SuspenseList>
</div>
);
}

const errors = [];
await serverAct(async () => {
const {pipe} = ReactDOMFizzServer.renderToPipeableStream(<Foo />, {
onError(error) {
errors.push(error.message);
},
});
pipe(writable);
});

assertLog([
'Suspend! [A]',
'B', // TODO: Defer rendering the content after fallback if previous suspended,
'Loading A',
'Loading B',
]);

expect(getVisibleChildren(container)).toEqual(
<div>
<span>Loading A</span>
<span>Loading B</span>
</div>,
);

await serverAct(async () => {
A.reject(new Error('hi'));
});

assertLog(['Error! [hi]']);

expect(getVisibleChildren(container)).toEqual(
<div>
<span>Loading A</span>
<span>B</span>
</div>,
);

expect(errors).toEqual(['hi']);
expect(hasErrored).toBe(false);
expect(hasCompleted).toBe(true);
});
});
48 changes: 33 additions & 15 deletions packages/react-server/src/ReactFizzServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1783,6 +1783,8 @@ function renderSuspenseListRows(
): void {
// This is a fork of renderChildrenArray that's aware of tracking rows.
const prevKeyPath = task.keyPath;
const prevTreeContext = task.treeContext;
const prevRow = task.row;
const previousComponentStack = task.componentStack;
let previousDebugTask = null;
if (__DEV__) {
Expand All @@ -1792,10 +1794,9 @@ function renderSuspenseListRows(
pushServerComponentStack(task, (task.node: any).props.children._debugInfo);
}

const prevTreeContext = task.treeContext;
const prevRow = task.row;
const totalChildren = rows.length;
task.keyPath = keyPath;

const totalChildren = rows.length;
let previousSuspenseListRow: null | SuspenseListRow = null;
if (task.replay !== null) {
// Replay
Expand Down Expand Up @@ -4392,6 +4393,14 @@ function erroredTask(
encodeErrorForBoundary(boundary, errorDigest, error, errorInfo, false);
untrackBoundary(request, boundary);

const boundaryRow = boundary.row;
if (boundaryRow !== null) {
// Unblock the SuspenseListRow that was blocked by this boundary.
if (--boundaryRow.pendingTasks === 0) {
finishSuspenseListRow(request, boundaryRow);
}
}

// Regardless of what happens next, this boundary won't be displayed,
// so we can flush it, if the parent already flushed.
if (boundary.parentFlushed) {
Expand Down Expand Up @@ -4544,13 +4553,6 @@ function abortTask(task: Task, request: Request, error: mixed): void {
segment.status = ABORTED;
}

const row = task.row;
if (row !== null) {
if (--row.pendingTasks === 0) {
finishSuspenseListRow(request, row);
}
}

const errorInfo = getThrownInfo(task.componentStack);

if (boundary === null) {
Expand All @@ -4573,7 +4575,7 @@ function abortTask(task: Task, request: Request, error: mixed): void {
// we just need to mark it as postponed.
logPostpone(request, postponeInstance.message, errorInfo, null);
trackPostpone(request, trackedPostpones, task, segment);
finishedTask(request, null, row, segment);
finishedTask(request, null, task.row, segment);
} else {
const fatal = new Error(
'The render was aborted with postpone when the shell is incomplete. Reason: ' +
Expand All @@ -4592,7 +4594,7 @@ function abortTask(task: Task, request: Request, error: mixed): void {
// We log the error but we still resolve the prerender
logRecoverableError(request, error, errorInfo, null);
trackPostpone(request, trackedPostpones, task, segment);
finishedTask(request, null, row, segment);
finishedTask(request, null, task.row, segment);
} else {
logRecoverableError(request, error, errorInfo, null);
fatalError(request, error, errorInfo, null);
Expand Down Expand Up @@ -4636,7 +4638,6 @@ function abortTask(task: Task, request: Request, error: mixed): void {
}
}
} else {
boundary.pendingTasks--;
// We construct an errorInfo from the boundary's componentStack so the error in dev will indicate which
// boundary the message is referring to
const trackedPostpones = request.trackedPostpones;
Expand Down Expand Up @@ -4664,7 +4665,7 @@ function abortTask(task: Task, request: Request, error: mixed): void {
abortTask(fallbackTask, request, error),
);
boundary.fallbackAbortableTasks.clear();
return finishedTask(request, boundary, row, segment);
return finishedTask(request, boundary, task.row, segment);
}
}
boundary.status = CLIENT_RENDERED;
Expand All @@ -4681,7 +4682,7 @@ function abortTask(task: Task, request: Request, error: mixed): void {
logPostpone(request, postponeInstance.message, errorInfo, null);
if (request.trackedPostpones !== null && segment !== null) {
trackPostpone(request, request.trackedPostpones, task, segment);
finishedTask(request, task.blockedBoundary, row, segment);
finishedTask(request, task.blockedBoundary, task.row, segment);

// If this boundary was still pending then we haven't already cancelled its fallbacks.
// We'll need to abort the fallbacks, which will also error that parent boundary.
Expand All @@ -4706,6 +4707,16 @@ function abortTask(task: Task, request: Request, error: mixed): void {
}
}

boundary.pendingTasks--;

const boundaryRow = boundary.row;
if (boundaryRow !== null) {
// Unblock the SuspenseListRow that was blocked by this boundary.
if (--boundaryRow.pendingTasks === 0) {
finishSuspenseListRow(request, boundaryRow);
}
}

// If this boundary was still pending then we haven't already cancelled its fallbacks.
// We'll need to abort the fallbacks, which will also error that parent boundary.
boundary.fallbackAbortableTasks.forEach(fallbackTask =>
Expand All @@ -4714,6 +4725,13 @@ function abortTask(task: Task, request: Request, error: mixed): void {
boundary.fallbackAbortableTasks.clear();
}

const row = task.row;
if (row !== null) {
if (--row.pendingTasks === 0) {
finishSuspenseListRow(request, row);
}
}

request.allPendingTasks--;
if (request.allPendingTasks === 0) {
completeAll(request);
Expand Down
Loading