GeezerVison is showing github.com — Abort Flight (#24754) · react/react@56389e8
File treeExpand file treeCollapse file tree Open diff view settings react-server-dom-relay/src react-server-dom-webpack/src react-server-native-relay/src
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/react-server-dom-relay/src/ReactFlightDOMRelayServerHostConfig.js +8Lines changed: 8 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -117,6 +117,14 @@ export function processModelChunk( | 117 | 117 | return ['J', id, json]; | 118 | 118 | } | 119 | 119 |
| | 120 | +export function processReferenceChunk( | | 121 | + request: Request, | | 122 | + id: number, | | 123 | + reference: string, | | 124 | +): Chunk { | | 125 | + return ['J', id, reference]; | | 126 | +} | | 127 | + | 120 | 128 | export function processModuleChunk( | 121 | 129 | request: Request, | 122 | 130 | id: number, | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-server-dom-webpack/src/ReactFlightDOMServerBrowser.js +16-2Lines changed: 16 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -15,12 +15,14 @@ import { | 15 | 15 | createRequest, | 16 | 16 | startWork, | 17 | 17 | startFlowing, | | 18 | + abort, | 18 | 19 | } from 'react-server/src/ReactFlightServer'; | 19 | 20 |
| 20 | 21 | type Options = { | 21 | | - onError?: (error: mixed) => void, | 22 | | - context?: Array<[string, ServerContextJSONValue]>, | 23 | 22 | identifierPrefix?: string, | | 23 | + signal?: AbortSignal, | | 24 | + context?: Array<[string, ServerContextJSONValue]>, | | 25 | + onError?: (error: mixed) => void, | 24 | 26 | }; | 25 | 27 |
| 26 | 28 | function renderToReadableStream( | @@ -35,6 +37,18 @@ function renderToReadableStream( | 35 | 37 | options ? options.context : undefined, | 36 | 38 | options ? options.identifierPrefix : undefined, | 37 | 39 | ); | | 40 | + if (options && options.signal) { | | 41 | + const signal = options.signal; | | 42 | + if (signal.aborted) { | | 43 | + abort(request, (signal: any).reason); | | 44 | + } else { | | 45 | + const listener = () => { | | 46 | + abort(request, (signal: any).reason); | | 47 | + signal.removeEventListener('abort', listener); | | 48 | + }; | | 49 | + signal.addEventListener('abort', listener); | | 50 | + } | | 51 | + } | 38 | 52 | const stream = new ReadableStream( | 39 | 53 | { | 40 | 54 | type: 'bytes', | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-server-dom-webpack/src/ReactFlightDOMServerNode.js +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -16,6 +16,7 @@ import { | 16 | 16 | createRequest, | 17 | 17 | startWork, | 18 | 18 | startFlowing, | | 19 | + abort, | 19 | 20 | } from 'react-server/src/ReactFlightServer'; | 20 | 21 |
| 21 | 22 | function createDrainHandler(destination, request) { | @@ -29,6 +30,7 @@ type Options = { | 29 | 30 | }; | 30 | 31 |
| 31 | 32 | type PipeableStream = {| | | 33 | + abort(reason: mixed): void, | 32 | 34 | pipe<T: Writable>(destination: T): T, | 33 | 35 | |}; | 34 | 36 |
| @@ -58,6 +60,9 @@ function renderToPipeableStream( | 58 | 60 | destination.on('drain', createDrainHandler(destination, request)); | 59 | 61 | return destination; | 60 | 62 | }, | | 63 | + abort(reason: mixed) { | | 64 | + abort(request, reason); | | 65 | + }, | 61 | 66 | }; | 62 | 67 | } | 63 | 68 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/react-server-dom-webpack/src/__tests__/ReactFlightDOM-test.js +67-9Lines changed: 67 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -30,6 +30,7 @@ let React; | 30 | 30 | let ReactDOMClient; | 31 | 31 | let ReactServerDOMWriter; | 32 | 32 | let ReactServerDOMReader; | | 33 | +let Suspense; | 33 | 34 |
| 34 | 35 | describe('ReactFlightDOM', () => { | 35 | 36 | beforeEach(() => { | @@ -42,6 +43,7 @@ describe('ReactFlightDOM', () => { | 42 | 43 | ReactDOMClient = require('react-dom/client'); | 43 | 44 | ReactServerDOMWriter = require('react-server-dom-webpack/writer.node.server'); | 44 | 45 | ReactServerDOMReader = require('react-server-dom-webpack'); | | 46 | + Suspense = React.Suspense; | 45 | 47 | }); | 46 | 48 |
| 47 | 49 | function getTestStream() { | @@ -92,6 +94,11 @@ describe('ReactFlightDOM', () => { | 92 | 94 | } | 93 | 95 | } | 94 | 96 |
| | 97 | + const theInfinitePromise = new Promise(() => {}); | | 98 | + function InfiniteSuspend() { | | 99 | + throw theInfinitePromise; | | 100 | + } | | 101 | + | 95 | 102 | it('should resolve HTML using Node streams', async () => { | 96 | 103 | function Text({children}) { | 97 | 104 | return <span>{children}</span>; | @@ -133,8 +140,6 @@ describe('ReactFlightDOM', () => { | 133 | 140 | }); | 134 | 141 |
| 135 | 142 | it('should resolve the root', async () => { | 136 | | - const {Suspense} = React; | 137 | | - | 138 | 143 | // Model | 139 | 144 | function Text({children}) { | 140 | 145 | return <span>{children}</span>; | @@ -184,8 +189,6 @@ describe('ReactFlightDOM', () => { | 184 | 189 | }); | 185 | 190 |
| 186 | 191 | it('should not get confused by $', async () => { | 187 | | - const {Suspense} = React; | 188 | | - | 189 | 192 | // Model | 190 | 193 | function RootModel() { | 191 | 194 | return {text: '$1'}; | @@ -220,8 +223,6 @@ describe('ReactFlightDOM', () => { | 220 | 223 | }); | 221 | 224 |
| 222 | 225 | it('should not get confused by @', async () => { | 223 | | - const {Suspense} = React; | 224 | | - | 225 | 226 | // Model | 226 | 227 | function RootModel() { | 227 | 228 | return {text: '@div'}; | @@ -257,7 +258,6 @@ describe('ReactFlightDOM', () => { | 257 | 258 |
| 258 | 259 | it('should progressively reveal server components', async () => { | 259 | 260 | let reportedErrors = []; | 260 | | - const {Suspense} = React; | 261 | 261 |
| 262 | 262 | // Client Components | 263 | 263 |
| @@ -460,8 +460,6 @@ describe('ReactFlightDOM', () => { | 460 | 460 | }); | 461 | 461 |
| 462 | 462 | it('should preserve state of client components on refetch', async () => { | 463 | | - const {Suspense} = React; | 464 | | - | 465 | 463 | // Client | 466 | 464 |
| 467 | 465 | function Page({response}) { | @@ -545,4 +543,64 @@ describe('ReactFlightDOM', () => { | 545 | 543 | expect(inputB.tagName).toBe('INPUT'); | 546 | 544 | expect(inputB.value).toBe('goodbye'); | 547 | 545 | }); | | 546 | + | | 547 | + it('should be able to complete after aborting and throw the reason client-side', async () => { | | 548 | + const reportedErrors = []; | | 549 | + | | 550 | + class ErrorBoundary extends React.Component { | | 551 | + state = {hasError: false, error: null}; | | 552 | + static getDerivedStateFromError(error) { | | 553 | + return { | | 554 | + hasError: true, | | 555 | + error, | | 556 | + }; | | 557 | + } | | 558 | + render() { | | 559 | + if (this.state.hasError) { | | 560 | + return this.props.fallback(this.state.error); | | 561 | + } | | 562 | + return this.props.children; | | 563 | + } | | 564 | + } | | 565 | + | | 566 | + const {writable, readable} = getTestStream(); | | 567 | + const {pipe, abort} = ReactServerDOMWriter.renderToPipeableStream( | | 568 | + <div> | | 569 | + <InfiniteSuspend /> | | 570 | + </div>, | | 571 | + webpackMap, | | 572 | + { | | 573 | + onError(x) { | | 574 | + reportedErrors.push(x); | | 575 | + }, | | 576 | + }, | | 577 | + ); | | 578 | + pipe(writable); | | 579 | + const response = ReactServerDOMReader.createFromReadableStream(readable); | | 580 | + | | 581 | + const container = document.createElement('div'); | | 582 | + const root = ReactDOMClient.createRoot(container); | | 583 | + | | 584 | + function App({res}) { | | 585 | + return res.readRoot(); | | 586 | + } | | 587 | + | | 588 | + await act(async () => { | | 589 | + root.render( | | 590 | + <ErrorBoundary fallback={e => <p>{e.message}</p>}> | | 591 | + <Suspense fallback={<p>(loading)</p>}> | | 592 | + <App res={response} /> | | 593 | + </Suspense> | | 594 | + </ErrorBoundary>, | | 595 | + ); | | 596 | + }); | | 597 | + expect(container.innerHTML).toBe('<p>(loading)</p>'); | | 598 | + | | 599 | + await act(async () => { | | 600 | + abort('for reasons'); | | 601 | + }); | | 602 | + expect(container.innerHTML).toBe('<p>Error: for reasons</p>'); | | 603 | + | | 604 | + expect(reportedErrors).toEqual(['for reasons']); | | 605 | + }); | 548 | 606 | }); |
Collapse file Copy file name to clipboardExpand all lines: packages/react-server-dom-webpack/src/__tests__/ReactFlightDOMBrowser-test.js +71-3Lines changed: 71 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -27,6 +27,7 @@ let ReactDOMClient; | 27 | 27 | let ReactDOMServer; | 28 | 28 | let ReactServerDOMWriter; | 29 | 29 | let ReactServerDOMReader; | | 30 | +let Suspense; | 30 | 31 |
| 31 | 32 | describe('ReactFlightDOMBrowser', () => { | 32 | 33 | beforeEach(() => { | @@ -39,6 +40,7 @@ describe('ReactFlightDOMBrowser', () => { | 39 | 40 | ReactDOMServer = require('react-dom/server.browser'); | 40 | 41 | ReactServerDOMWriter = require('react-server-dom-webpack/writer.browser.server'); | 41 | 42 | ReactServerDOMReader = require('react-server-dom-webpack'); | | 43 | + Suspense = React.Suspense; | 42 | 44 | }); | 43 | 45 |
| 44 | 46 | function moduleReference(moduleExport) { | @@ -108,6 +110,11 @@ describe('ReactFlightDOMBrowser', () => { | 108 | 110 | return [DelayedText, _resolve, _reject]; | 109 | 111 | } | 110 | 112 |
| | 113 | + const theInfinitePromise = new Promise(() => {}); | | 114 | + function InfiniteSuspend() { | | 115 | + throw theInfinitePromise; | | 116 | + } | | 117 | + | 111 | 118 | it('should resolve HTML using W3C streams', async () => { | 112 | 119 | function Text({children}) { | 113 | 120 | return <span>{children}</span>; | @@ -180,7 +187,6 @@ describe('ReactFlightDOMBrowser', () => { | 180 | 187 |
| 181 | 188 | it('should progressively reveal server components', async () => { | 182 | 189 | let reportedErrors = []; | 183 | | - const {Suspense} = React; | 184 | 190 |
| 185 | 191 | // Client Components | 186 | 192 |
| @@ -356,8 +362,6 @@ describe('ReactFlightDOMBrowser', () => { | 356 | 362 | }); | 357 | 363 |
| 358 | 364 | it('should close the stream upon completion when rendering to W3C streams', async () => { | 359 | | - const {Suspense} = React; | 360 | | - | 361 | 365 | // Model | 362 | 366 | function Text({children}) { | 363 | 367 | return children; | @@ -512,4 +516,68 @@ describe('ReactFlightDOMBrowser', () => { | 512 | 516 | const result = await readResult(ssrStream); | 513 | 517 | expect(result).toEqual('<span>Client Component</span>'); | 514 | 518 | }); | | 519 | + | | 520 | + it('should be able to complete after aborting and throw the reason client-side', async () => { | | 521 | + const reportedErrors = []; | | 522 | + | | 523 | + class ErrorBoundary extends React.Component { | | 524 | + state = {hasError: false, error: null}; | | 525 | + static getDerivedStateFromError(error) { | | 526 | + return { | | 527 | + hasError: true, | | 528 | + error, | | 529 | + }; | | 530 | + } | | 531 | + render() { | | 532 | + if (this.state.hasError) { | | 533 | + return this.props.fallback(this.state.error); | | 534 | + } | | 535 | + return this.props.children; | | 536 | + } | | 537 | + } | | 538 | + | | 539 | + const controller = new AbortController(); | | 540 | + const stream = ReactServerDOMWriter.renderToReadableStream( | | 541 | + <div> | | 542 | + <InfiniteSuspend /> | | 543 | + </div>, | | 544 | + webpackMap, | | 545 | + { | | 546 | + signal: controller.signal, | | 547 | + onError(x) { | | 548 | + reportedErrors.push(x); | | 549 | + }, | | 550 | + }, | | 551 | + ); | | 552 | + const response = ReactServerDOMReader.createFromReadableStream(stream); | | 553 | + | | 554 | + const container = document.createElement('div'); | | 555 | + const root = ReactDOMClient.createRoot(container); | | 556 | + | | 557 | + function App({res}) { | | 558 | + return res.readRoot(); | | 559 | + } | | 560 | + | | 561 | + await act(async () => { | | 562 | + root.render( | | 563 | + <ErrorBoundary fallback={e => <p>{e.message}</p>}> | | 564 | + <Suspense fallback={<p>(loading)</p>}> | | 565 | + <App res={response} /> | | 566 | + </Suspense> | | 567 | + </ErrorBoundary>, | | 568 | + ); | | 569 | + }); | | 570 | + expect(container.innerHTML).toBe('<p>(loading)</p>'); | | 571 | + | | 572 | + await act(async () => { | | 573 | + // @TODO this is a hack to work around lack of support for abortSignal.reason in node | | 574 | + // The abort call itself should set this property but since we are testing in node we | | 575 | + // set it here manually | | 576 | + controller.signal.reason = 'for reasons'; | | 577 | + controller.abort('for reasons'); | | 578 | + }); | | 579 | + expect(container.innerHTML).toBe('<p>Error: for reasons</p>'); | | 580 | + | | 581 | + expect(reportedErrors).toEqual(['for reasons']); | | 582 | + }); | 515 | 583 | }); |
Collapse file Copy file name to clipboardExpand all lines: packages/react-server-native-relay/src/ReactFlightNativeRelayServerHostConfig.js +8Lines changed: 8 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -114,6 +114,14 @@ export function processModelChunk( | 114 | 114 | return ['J', id, json]; | 115 | 115 | } | 116 | 116 |
| | 117 | +export function processReferenceChunk( | | 118 | + request: Request, | | 119 | + id: number, | | 120 | + reference: string, | | 121 | +): Chunk { | | 122 | + return ['J', id, reference]; | | 123 | +} | | 124 | + | 117 | 125 | export function processModuleChunk( | 118 | 126 | request: Request, | 119 | 127 | id: number, | |
|