GeezerVison is showing github.com — [Fizz] Expose a method to abort a pending request (#21027) · react/react@cf485e6
File treeExpand file treeCollapse file tree Open diff view settings fixtures/fizz-ssr-browser
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: fixtures/fizz-ssr-browser/index.html +4-1Lines changed: 4 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -20,7 +20,10 @@ <h1>Fizz Example</h1> | 20 | 20 | <script src="../../build/node_modules/react-dom/umd/react-dom-unstable-fizz.browser.development.js"></script> | 21 | 21 | <script src="https://unpkg.com/babel-standalone@6/babel.js"></script> | 22 | 22 | <script type="text/babel"> | 23 | | - let stream = ReactDOMFizzServer.renderToReadableStream(<body>Success</body>); | | 23 | + let controller = new AbortController(); | | 24 | + let stream = ReactDOMFizzServer.renderToReadableStream(<body>Success</body>, { | | 25 | + signal: controller.signal, | | 26 | + }); | 24 | 27 | let response = new Response(stream, { | 25 | 28 | headers: {'Content-Type': 'text/html'}, | 26 | 29 | }); | |
Collapse file Copy file name to clipboardExpand all lines: package.json +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 36 | 36 | "@babel/preset-react": "^7.10.4", | 37 | 37 | "@babel/traverse": "^7.11.0", | 38 | 38 | "@mattiasbuelens/web-streams-polyfill": "^0.3.2", | | 39 | + "abort-controller": "^3.0.0", | 39 | 40 | "art": "0.10.1", | 40 | 41 | "babel-eslint": "^10.0.3", | 41 | 42 | "babel-plugin-syntax-trailing-function-commas": "^6.5.0", | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/__tests__/ReactDOMFizzServer-test.js +51Lines changed: 51 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -263,4 +263,55 @@ describe('ReactDOMFizzServer', () => { | 263 | 263 | // Now it's hydrated. | 264 | 264 | expect(ref.current).toBe(h1); | 265 | 265 | }); | | 266 | + | | 267 | + // @gate experimental | | 268 | + it('client renders a boundary if it does not resolve before aborting', async () => { | | 269 | + function App() { | | 270 | + return ( | | 271 | + <div> | | 272 | + <Suspense fallback="Loading..."> | | 273 | + <h1> | | 274 | + <AsyncText text="Hello" /> | | 275 | + </h1> | | 276 | + </Suspense> | | 277 | + </div> | | 278 | + ); | | 279 | + } | | 280 | + | | 281 | + let controls; | | 282 | + await act(async () => { | | 283 | + controls = ReactDOMFizzServer.pipeToNodeWritable(<App />, writable); | | 284 | + }); | | 285 | + | | 286 | + // We're still showing a fallback. | | 287 | + | | 288 | + // Attempt to hydrate the content. | | 289 | + const root = ReactDOM.unstable_createRoot(container, {hydrate: true}); | | 290 | + root.render(<App />); | | 291 | + Scheduler.unstable_flushAll(); | | 292 | + | | 293 | + // We're still loading because we're waiting for the server to stream more content. | | 294 | + expect(getVisibleChildren(container)).toEqual(<div>Loading...</div>); | | 295 | + | | 296 | + // We abort the server response. | | 297 | + await act(async () => { | | 298 | + controls.abort(); | | 299 | + }); | | 300 | + | | 301 | + // We still can't render it on the client. | | 302 | + Scheduler.unstable_flushAll(); | | 303 | + expect(getVisibleChildren(container)).toEqual(<div>Loading...</div>); | | 304 | + | | 305 | + // We now resolve it on the client. | | 306 | + resolveText('Hello'); | | 307 | + | | 308 | + Scheduler.unstable_flushAll(); | | 309 | + | | 310 | + // The client rendered HTML is now in place. | | 311 | + expect(getVisibleChildren(container)).toEqual( | | 312 | + <div> | | 313 | + <h1>Hello</h1> | | 314 | + </div>, | | 315 | + ); | | 316 | + }); | 266 | 317 | }); |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/__tests__/ReactDOMFizzServerBrowser-test.js +19Lines changed: 19 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 12 | 12 | // Polyfills for test environment | 13 | 13 | global.ReadableStream = require('@mattiasbuelens/web-streams-polyfill/ponyfill/es6').ReadableStream; | 14 | 14 | global.TextEncoder = require('util').TextEncoder; | | 15 | +global.AbortController = require('abort-controller'); | 15 | 16 |
| 16 | 17 | let React; | 17 | 18 | let ReactDOMFizzServer; | @@ -110,4 +111,22 @@ describe('ReactDOMFizzServer', () => { | 110 | 111 | const result = await readResult(stream); | 111 | 112 | expect(result).toContain('Loading'); | 112 | 113 | }); | | 114 | + | | 115 | + // @gate experimental | | 116 | + it('should be able to complete by aborting even if the promise never resolves', async () => { | | 117 | + const controller = new AbortController(); | | 118 | + const stream = ReactDOMFizzServer.renderToReadableStream( | | 119 | + <div> | | 120 | + <Suspense fallback={<div>Loading</div>}> | | 121 | + <InfiniteSuspend /> | | 122 | + </Suspense> | | 123 | + </div>, | | 124 | + {signal: controller.signal}, | | 125 | + ); | | 126 | + | | 127 | + controller.abort(); | | 128 | + | | 129 | + const result = await readResult(stream); | | 130 | + expect(result).toContain('Loading'); | | 131 | + }); | 113 | 132 | }); |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/__tests__/ReactDOMFizzServerNode-test.js +50Lines changed: 50 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -115,4 +115,54 @@ describe('ReactDOMFizzServer', () => { | 115 | 115 | expect(output.error).toBe(undefined); | 116 | 116 | expect(output.result).toContain('Loading'); | 117 | 117 | }); | | 118 | + | | 119 | + // @gate experimental | | 120 | + it('should not attempt to render the fallback if the main content completes first', async () => { | | 121 | + const {writable, output, completed} = getTestWritable(); | | 122 | + | | 123 | + let renderedFallback = false; | | 124 | + function Fallback() { | | 125 | + renderedFallback = true; | | 126 | + return 'Loading...'; | | 127 | + } | | 128 | + function Content() { | | 129 | + return 'Hi'; | | 130 | + } | | 131 | + ReactDOMFizzServer.pipeToNodeWritable( | | 132 | + <Suspense fallback={<Fallback />}> | | 133 | + <Content /> | | 134 | + </Suspense>, | | 135 | + writable, | | 136 | + ); | | 137 | + | | 138 | + await completed; | | 139 | + | | 140 | + expect(output.result).toContain('Hi'); | | 141 | + expect(output.result).not.toContain('Loading'); | | 142 | + expect(renderedFallback).toBe(false); | | 143 | + }); | | 144 | + | | 145 | + // @gate experimental | | 146 | + it('should be able to complete by aborting even if the promise never resolves', async () => { | | 147 | + const {writable, output, completed} = getTestWritable(); | | 148 | + const {abort} = ReactDOMFizzServer.pipeToNodeWritable( | | 149 | + <div> | | 150 | + <Suspense fallback={<div>Loading</div>}> | | 151 | + <InfiniteSuspend /> | | 152 | + </Suspense> | | 153 | + </div>, | | 154 | + writable, | | 155 | + ); | | 156 | + | | 157 | + jest.runAllTimers(); | | 158 | + | | 159 | + expect(output.result).toContain('Loading'); | | 160 | + | | 161 | + abort(); | | 162 | + | | 163 | + await completed; | | 164 | + | | 165 | + expect(output.error).toBe(undefined); | | 166 | + expect(output.result).toContain('Loading'); | | 167 | + }); | 118 | 168 | }); |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/server/ReactDOMFizzServerBrowser.js +17-1Lines changed: 17 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -13,10 +13,26 @@ import { | 13 | 13 | createRequest, | 14 | 14 | startWork, | 15 | 15 | startFlowing, | | 16 | + abort, | 16 | 17 | } from 'react-server/src/ReactFizzServer'; | 17 | 18 |
| 18 | | -function renderToReadableStream(children: ReactNodeList): ReadableStream { | | 19 | +type Options = { | | 20 | + signal?: AbortSignal, | | 21 | +}; | | 22 | + | | 23 | +function renderToReadableStream( | | 24 | + children: ReactNodeList, | | 25 | + options?: Options, | | 26 | +): ReadableStream { | 19 | 27 | let request; | | 28 | + if (options && options.signal) { | | 29 | + const signal = options.signal; | | 30 | + const listener = () => { | | 31 | + abort(request); | | 32 | + signal.removeEventListener('abort', listener); | | 33 | + }; | | 34 | + signal.addEventListener('abort', listener); | | 35 | + } | 20 | 36 | return new ReadableStream({ | 21 | 37 | start(controller) { | 22 | 38 | request = createRequest(children, controller); | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/server/ReactDOMFizzServerNode.js +13-1Lines changed: 13 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -14,19 +14,31 @@ import { | 14 | 14 | createRequest, | 15 | 15 | startWork, | 16 | 16 | startFlowing, | | 17 | + abort, | 17 | 18 | } from 'react-server/src/ReactFizzServer'; | 18 | 19 |
| 19 | 20 | function createDrainHandler(destination, request) { | 20 | 21 | return () => startFlowing(request); | 21 | 22 | } | 22 | 23 |
| | 24 | +type Controls = { | | 25 | + // Cancel any pending I/O and put anything remaining into | | 26 | + // client rendered mode. | | 27 | + abort(): void, | | 28 | +}; | | 29 | + | 23 | 30 | function pipeToNodeWritable( | 24 | 31 | children: ReactNodeList, | 25 | 32 | destination: Writable, | 26 | | -): void { | | 33 | +): Controls { | 27 | 34 | const request = createRequest(children, destination); | 28 | 35 | destination.on('drain', createDrainHandler(destination, request)); | 29 | 36 | startWork(request); | | 37 | + return { | | 38 | + abort() { | | 39 | + abort(request); | | 40 | + }, | | 41 | + }; | 30 | 42 | } | 31 | 43 |
| 32 | 44 | export {pipeToNodeWritable}; |
Collapse file Copy file name to clipboardExpand all lines: packages/react-noop-renderer/src/ReactNoopServer.js +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -216,6 +216,9 @@ function render(children: React$Element<any>): Destination { | 216 | 216 | placeholders: new Map(), | 217 | 217 | segments: new Map(), | 218 | 218 | stack: [], | | 219 | + abort() { | | 220 | + ReactNoopServer.abort(request); | | 221 | + }, | 219 | 222 | }; | 220 | 223 | const request = ReactNoopServer.createRequest(children, destination); | 221 | 224 | ReactNoopServer.startWork(request); | |
|