Skip to content

Commit 85180b8

Browse files
authored
[Fizz][Static] when aborting a prerender halt unfinished boundaries instead of erroring (facebook#30732)
When we introduced prerendering for flight we modeled an abort of a flight prerender as having unfinished rows. This is similar to how postpone was already implemented when you postponed from "within" a prerender using React.unstable_postpone. However when aborting with a postponed instance every boundary would be eagerly marked for client rendering which is more akin to prerendering and then resuming with an aborted signal. The insight with the flight work was that it's not so much the postpone that describes the intended semantics but the abort combined with a prerender. So like in flight when you abort a prerender and enableHalt is enabled boundaries and the shell won't error for any reason. Fizz will still call onPostpone and onError according to the abort reason but the consuemr of the prerender should expect to resume it before trying to use it.
1 parent 4c2dfb3 commit 85180b8

File tree

5 files changed

+424
-21
lines changed

5 files changed

+424
-21
lines changed

packages/react-dom/src/__tests__/ReactDOMFizzServer-test.js

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7746,6 +7746,112 @@ describe('ReactDOMFizzServer', () => {
77467746
);
77477747
});
77487748

7749+
// @gate enableHalt
7750+
it('can resume a prerender that was aborted', async () => {
7751+
const promise = new Promise(r => {});
7752+
7753+
let prerendering = true;
7754+
7755+
function Wait() {
7756+
if (prerendering) {
7757+
return React.use(promise);
7758+
} else {
7759+
return 'Hello';
7760+
}
7761+
}
7762+
7763+
function App() {
7764+
return (
7765+
<div>
7766+
<Suspense fallback="Loading...">
7767+
<p>
7768+
<span>
7769+
<Suspense fallback="Loading again...">
7770+
<Wait />
7771+
</Suspense>
7772+
</span>
7773+
</p>
7774+
<p>
7775+
<span>
7776+
<Suspense fallback="Loading again too...">
7777+
<Wait />
7778+
</Suspense>
7779+
</span>
7780+
</p>
7781+
</Suspense>
7782+
</div>
7783+
);
7784+
}
7785+
7786+
const controller = new AbortController();
7787+
const signal = controller.signal;
7788+
7789+
const errors = [];
7790+
function onError(error) {
7791+
errors.push(error);
7792+
}
7793+
let pendingPrerender;
7794+
await act(() => {
7795+
pendingPrerender = ReactDOMFizzStatic.prerenderToNodeStream(<App />, {
7796+
signal,
7797+
onError,
7798+
});
7799+
});
7800+
controller.abort('boom');
7801+
7802+
const prerendered = await pendingPrerender;
7803+
7804+
expect(errors).toEqual(['boom', 'boom']);
7805+
7806+
const preludeWritable = new Stream.PassThrough();
7807+
preludeWritable.setEncoding('utf8');
7808+
preludeWritable.on('data', chunk => {
7809+
writable.write(chunk);
7810+
});
7811+
7812+
await act(() => {
7813+
prerendered.prelude.pipe(preludeWritable);
7814+
});
7815+
7816+
expect(getVisibleChildren(container)).toEqual(
7817+
<div>
7818+
<p>
7819+
<span>Loading again...</span>
7820+
</p>
7821+
<p>
7822+
<span>Loading again too...</span>
7823+
</p>
7824+
</div>,
7825+
);
7826+
7827+
prerendering = false;
7828+
7829+
errors.length = 0;
7830+
const resumed = await ReactDOMFizzServer.resumeToPipeableStream(
7831+
<App />,
7832+
JSON.parse(JSON.stringify(prerendered.postponed)),
7833+
{
7834+
onError,
7835+
},
7836+
);
7837+
7838+
await act(() => {
7839+
resumed.pipe(writable);
7840+
});
7841+
7842+
expect(errors).toEqual([]);
7843+
expect(getVisibleChildren(container)).toEqual(
7844+
<div>
7845+
<p>
7846+
<span>Hello</span>
7847+
</p>
7848+
<p>
7849+
<span>Hello</span>
7850+
</p>
7851+
</div>,
7852+
);
7853+
});
7854+
77497855
// @gate enablePostpone
77507856
it('does not call onError when you abort with a postpone instance during resume', async () => {
77517857
let prerendering = true;

packages/react-dom/src/__tests__/ReactDOMFizzStatic-test.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,4 +454,56 @@ describe('ReactDOMFizzStatic', () => {
454454
});
455455
expect(getVisibleChildren(container)).toEqual(undefined);
456456
});
457+
458+
// @gate enableHalt
459+
it('will halt a prerender when aborting with an error during a render', async () => {
460+
const controller = new AbortController();
461+
function App() {
462+
controller.abort('sync');
463+
return <div>hello world</div>;
464+
}
465+
466+
const errors = [];
467+
const result = await ReactDOMFizzStatic.prerenderToNodeStream(<App />, {
468+
signal: controller.signal,
469+
onError(error) {
470+
errors.push(error);
471+
},
472+
});
473+
await act(async () => {
474+
result.prelude.pipe(writable);
475+
});
476+
expect(errors).toEqual(['sync']);
477+
expect(getVisibleChildren(container)).toEqual(undefined);
478+
});
479+
480+
// @gate enableHalt
481+
it('will halt a prerender when aborting with an error in a microtask', async () => {
482+
const errors = [];
483+
484+
const controller = new AbortController();
485+
function App() {
486+
React.use(
487+
new Promise(() => {
488+
Promise.resolve().then(() => {
489+
controller.abort('async');
490+
});
491+
}),
492+
);
493+
return <div>hello world</div>;
494+
}
495+
496+
errors.length = 0;
497+
const result = await ReactDOMFizzStatic.prerenderToNodeStream(<App />, {
498+
signal: controller.signal,
499+
onError(error) {
500+
errors.push(error);
501+
},
502+
});
503+
await act(async () => {
504+
result.prelude.pipe(writable);
505+
});
506+
expect(errors).toEqual(['async']);
507+
expect(getVisibleChildren(container)).toEqual(undefined);
508+
});
457509
});

packages/react-dom/src/__tests__/ReactDOMFizzStaticBrowser-test.js

Lines changed: 92 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,8 @@ describe('ReactDOMFizzStaticBrowser', () => {
307307
});
308308

309309
// @gate experimental
310-
it('should reject if aborting before the shell is complete', async () => {
310+
// @gate !enableHalt
311+
it('should reject if aborting before the shell is complete and enableHalt is disabled', async () => {
311312
const errors = [];
312313
const controller = new AbortController();
313314
const promise = serverAct(() =>
@@ -339,6 +340,42 @@ describe('ReactDOMFizzStaticBrowser', () => {
339340
expect(errors).toEqual(['aborted for reasons']);
340341
});
341342

343+
// @gate enableHalt
344+
it('should resolve an empty prelude if aborting before the shell is complete', async () => {
345+
const errors = [];
346+
const controller = new AbortController();
347+
const promise = serverAct(() =>
348+
ReactDOMFizzStatic.prerender(
349+
<div>
350+
<InfiniteSuspend />
351+
</div>,
352+
{
353+
signal: controller.signal,
354+
onError(x) {
355+
errors.push(x.message);
356+
},
357+
},
358+
),
359+
);
360+
361+
await jest.runAllTimers();
362+
363+
const theReason = new Error('aborted for reasons');
364+
controller.abort(theReason);
365+
366+
let rejected = false;
367+
let prelude;
368+
try {
369+
({prelude} = await promise);
370+
} catch (error) {
371+
rejected = true;
372+
}
373+
expect(rejected).toBe(false);
374+
expect(errors).toEqual(['aborted for reasons']);
375+
const content = await readContent(prelude);
376+
expect(content).toBe('');
377+
});
378+
342379
// @gate experimental
343380
it('should be able to abort before something suspends', async () => {
344381
const errors = [];
@@ -365,18 +402,26 @@ describe('ReactDOMFizzStaticBrowser', () => {
365402
),
366403
);
367404

368-
let caughtError = null;
369-
try {
370-
await streamPromise;
371-
} catch (error) {
372-
caughtError = error;
405+
if (gate(flags => flags.enableHalt)) {
406+
const {prelude} = await streamPromise;
407+
const content = await readContent(prelude);
408+
expect(errors).toEqual(['The operation was aborted.']);
409+
expect(content).toBe('');
410+
} else {
411+
let caughtError = null;
412+
try {
413+
await streamPromise;
414+
} catch (error) {
415+
caughtError = error;
416+
}
417+
expect(caughtError.message).toBe('The operation was aborted.');
418+
expect(errors).toEqual(['The operation was aborted.']);
373419
}
374-
expect(caughtError.message).toBe('The operation was aborted.');
375-
expect(errors).toEqual(['The operation was aborted.']);
376420
});
377421

378422
// @gate experimental
379-
it('should reject if passing an already aborted signal', async () => {
423+
// @gate !enableHalt
424+
it('should reject if passing an already aborted signal and enableHalt is disabled', async () => {
380425
const errors = [];
381426
const controller = new AbortController();
382427
const theReason = new Error('aborted for reasons');
@@ -410,6 +455,44 @@ describe('ReactDOMFizzStaticBrowser', () => {
410455
expect(errors).toEqual(['aborted for reasons']);
411456
});
412457

458+
// @gate enableHalt
459+
it('should resolve an empty prelude if passing an already aborted signal', async () => {
460+
const errors = [];
461+
const controller = new AbortController();
462+
const theReason = new Error('aborted for reasons');
463+
controller.abort(theReason);
464+
465+
const promise = serverAct(() =>
466+
ReactDOMFizzStatic.prerender(
467+
<div>
468+
<Suspense fallback={<div>Loading</div>}>
469+
<InfiniteSuspend />
470+
</Suspense>
471+
</div>,
472+
{
473+
signal: controller.signal,
474+
onError(x) {
475+
errors.push(x.message);
476+
},
477+
},
478+
),
479+
);
480+
481+
// Technically we could still continue rendering the shell but currently the
482+
// semantics mean that we also abort any pending CPU work.
483+
let didThrow = false;
484+
let prelude;
485+
try {
486+
({prelude} = await promise);
487+
} catch (error) {
488+
didThrow = true;
489+
}
490+
expect(didThrow).toBe(false);
491+
expect(errors).toEqual(['aborted for reasons']);
492+
const content = await readContent(prelude);
493+
expect(content).toBe('');
494+
});
495+
413496
// @gate experimental
414497
it('supports custom abort reasons with a string', async () => {
415498
const promise = new Promise(r => {});

0 commit comments

Comments
 (0)