-
Notifications
You must be signed in to change notification settings - Fork 48.8k
[Fizz] Support basic SuspenseList forwards/backwards revealOrder #33306
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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c97a6d5
Test
sebmarkbage 8aac798
Track the row that we're rendering into as a context on the task
sebmarkbage 95ebd58
Track SuspenseListRow per row rendered
sebmarkbage e74fb07
Block rows from completing when there's still incomplete tasks within…
sebmarkbage 7e8aec6
Block a row from completing until a Suspense boundary completes
sebmarkbage 8ab5e1f
Use in fixture
sebmarkbage 662f9b5
Fix test that's now works properly
sebmarkbage 0c7f03f
Update packages/react-server/src/ReactFizzServer.js
sebmarkbage File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
327 changes: 327 additions & 0 deletions
327
packages/react-dom/src/__tests__/ReactDOMFizzSuspenseList-test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,327 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @emails react-core | ||
* @jest-environment ./scripts/jest/ReactDOMServerIntegrationEnvironment | ||
*/ | ||
|
||
'use strict'; | ||
import { | ||
insertNodesAndExecuteScripts, | ||
getVisibleChildren, | ||
} from '../test-utils/FizzTestUtils'; | ||
|
||
let JSDOM; | ||
let React; | ||
let Suspense; | ||
let SuspenseList; | ||
let assertLog; | ||
let Scheduler; | ||
let ReactDOMFizzServer; | ||
let Stream; | ||
let document; | ||
let writable; | ||
let container; | ||
let buffer = ''; | ||
let hasErrored = false; | ||
let fatalError = undefined; | ||
|
||
describe('ReactDOMFizSuspenseList', () => { | ||
beforeEach(() => { | ||
jest.resetModules(); | ||
JSDOM = require('jsdom').JSDOM; | ||
React = require('react'); | ||
assertLog = require('internal-test-utils').assertLog; | ||
ReactDOMFizzServer = require('react-dom/server'); | ||
Stream = require('stream'); | ||
|
||
Suspense = React.Suspense; | ||
SuspenseList = React.unstable_SuspenseList; | ||
|
||
Scheduler = require('scheduler'); | ||
|
||
// Test Environment | ||
const jsdom = new JSDOM( | ||
'<!DOCTYPE html><html><head></head><body><div id="container">', | ||
{ | ||
runScripts: 'dangerously', | ||
}, | ||
); | ||
document = jsdom.window.document; | ||
container = document.getElementById('container'); | ||
global.window = jsdom.window; | ||
// The Fizz runtime assumes requestAnimationFrame exists so we need to polyfill it. | ||
global.requestAnimationFrame = global.window.requestAnimationFrame = cb => | ||
setTimeout(cb); | ||
|
||
buffer = ''; | ||
hasErrored = false; | ||
|
||
writable = new Stream.PassThrough(); | ||
writable.setEncoding('utf8'); | ||
writable.on('data', chunk => { | ||
buffer += chunk; | ||
}); | ||
writable.on('error', error => { | ||
hasErrored = true; | ||
fatalError = error; | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
async function serverAct(callback) { | ||
await callback(); | ||
// Await one turn around the event loop. | ||
// This assumes that we'll flush everything we have so far. | ||
await new Promise(resolve => { | ||
setImmediate(resolve); | ||
}); | ||
if (hasErrored) { | ||
throw fatalError; | ||
} | ||
// JSDOM doesn't support stream HTML parser so we need to give it a proper fragment. | ||
// We also want to execute any scripts that are embedded. | ||
// We assume that we have now received a proper fragment of HTML. | ||
const bufferedContent = buffer; | ||
buffer = ''; | ||
const temp = document.createElement('body'); | ||
temp.innerHTML = bufferedContent; | ||
await insertNodesAndExecuteScripts(temp, container, null); | ||
jest.runAllTimers(); | ||
} | ||
|
||
function Text(props) { | ||
Scheduler.log(props.text); | ||
return <span>{props.text}</span>; | ||
} | ||
|
||
function createAsyncText(text) { | ||
let resolved = false; | ||
const Component = function () { | ||
if (!resolved) { | ||
Scheduler.log('Suspend! [' + text + ']'); | ||
throw promise; | ||
} | ||
return <Text text={text} />; | ||
}; | ||
const promise = new Promise(resolve => { | ||
Component.resolve = function () { | ||
resolved = true; | ||
return resolve(); | ||
}; | ||
}); | ||
return Component; | ||
} | ||
|
||
// @gate enableSuspenseList | ||
it('shows content independently by default', async () => { | ||
const A = createAsyncText('A'); | ||
const B = createAsyncText('B'); | ||
const C = createAsyncText('C'); | ||
|
||
function Foo() { | ||
return ( | ||
<div> | ||
<SuspenseList> | ||
<Suspense fallback={<Text text="Loading A" />}> | ||
<A /> | ||
</Suspense> | ||
<Suspense fallback={<Text text="Loading B" />}> | ||
<B /> | ||
</Suspense> | ||
<Suspense fallback={<Text text="Loading C" />}> | ||
<C /> | ||
</Suspense> | ||
</SuspenseList> | ||
</div> | ||
); | ||
} | ||
|
||
await A.resolve(); | ||
|
||
await serverAct(async () => { | ||
const {pipe} = ReactDOMFizzServer.renderToPipeableStream(<Foo />); | ||
pipe(writable); | ||
}); | ||
|
||
assertLog(['A', 'Suspend! [B]', 'Suspend! [C]', 'Loading B', 'Loading C']); | ||
|
||
expect(getVisibleChildren(container)).toEqual( | ||
<div> | ||
<span>A</span> | ||
<span>Loading B</span> | ||
<span>Loading C</span> | ||
</div>, | ||
); | ||
|
||
await serverAct(() => C.resolve()); | ||
assertLog(['C']); | ||
|
||
expect(getVisibleChildren(container)).toEqual( | ||
<div> | ||
<span>A</span> | ||
<span>Loading B</span> | ||
<span>C</span> | ||
</div>, | ||
); | ||
|
||
await serverAct(() => B.resolve()); | ||
assertLog(['B']); | ||
|
||
expect(getVisibleChildren(container)).toEqual( | ||
<div> | ||
<span>A</span> | ||
<span>B</span> | ||
<span>C</span> | ||
</div>, | ||
); | ||
}); | ||
|
||
// @gate enableSuspenseList | ||
it('displays each items in "forwards" order', async () => { | ||
const A = createAsyncText('A'); | ||
const B = createAsyncText('B'); | ||
const C = createAsyncText('C'); | ||
|
||
function Foo() { | ||
return ( | ||
<div> | ||
<SuspenseList revealOrder="forwards"> | ||
<Suspense fallback={<Text text="Loading A" />}> | ||
<A /> | ||
</Suspense> | ||
<Suspense fallback={<Text text="Loading B" />}> | ||
<B /> | ||
</Suspense> | ||
<Suspense fallback={<Text text="Loading C" />}> | ||
<C /> | ||
</Suspense> | ||
</SuspenseList> | ||
</div> | ||
); | ||
} | ||
|
||
await C.resolve(); | ||
|
||
await serverAct(async () => { | ||
const {pipe} = ReactDOMFizzServer.renderToPipeableStream(<Foo />); | ||
pipe(writable); | ||
}); | ||
|
||
assertLog([ | ||
'Suspend! [A]', | ||
'Suspend! [B]', // TODO: Defer rendering the content after fallback if previous suspended, | ||
'C', | ||
'Loading A', | ||
'Loading B', | ||
'Loading C', | ||
]); | ||
|
||
expect(getVisibleChildren(container)).toEqual( | ||
<div> | ||
<span>Loading A</span> | ||
<span>Loading B</span> | ||
<span>Loading C</span> | ||
</div>, | ||
); | ||
|
||
await serverAct(() => A.resolve()); | ||
assertLog(['A']); | ||
|
||
expect(getVisibleChildren(container)).toEqual( | ||
<div> | ||
<span>A</span> | ||
<span>Loading B</span> | ||
<span>Loading C</span> | ||
</div>, | ||
); | ||
|
||
await serverAct(() => B.resolve()); | ||
assertLog(['B']); | ||
|
||
expect(getVisibleChildren(container)).toEqual( | ||
<div> | ||
<span>A</span> | ||
<span>B</span> | ||
<span>C</span> | ||
</div>, | ||
); | ||
}); | ||
|
||
// @gate enableSuspenseList | ||
it('displays each items in "backwards" order', async () => { | ||
const A = createAsyncText('A'); | ||
const B = createAsyncText('B'); | ||
const C = createAsyncText('C'); | ||
|
||
function Foo() { | ||
return ( | ||
<div> | ||
<SuspenseList revealOrder="backwards"> | ||
<Suspense fallback={<Text text="Loading A" />}> | ||
<A /> | ||
</Suspense> | ||
<Suspense fallback={<Text text="Loading B" />}> | ||
<B /> | ||
</Suspense> | ||
<Suspense fallback={<Text text="Loading C" />}> | ||
<C /> | ||
</Suspense> | ||
</SuspenseList> | ||
</div> | ||
); | ||
} | ||
|
||
await A.resolve(); | ||
|
||
await serverAct(async () => { | ||
const {pipe} = ReactDOMFizzServer.renderToPipeableStream(<Foo />); | ||
pipe(writable); | ||
}); | ||
|
||
assertLog([ | ||
'Suspend! [C]', | ||
'Suspend! [B]', // TODO: Defer rendering the content after fallback if previous suspended, | ||
'A', | ||
'Loading C', | ||
'Loading B', | ||
'Loading A', | ||
]); | ||
|
||
expect(getVisibleChildren(container)).toEqual( | ||
<div> | ||
<span>Loading A</span> | ||
<span>Loading B</span> | ||
<span>Loading C</span> | ||
</div>, | ||
); | ||
|
||
await serverAct(() => C.resolve()); | ||
assertLog(['C']); | ||
|
||
expect(getVisibleChildren(container)).toEqual( | ||
<div> | ||
<span>Loading A</span> | ||
<span>Loading B</span> | ||
<span>C</span> | ||
</div>, | ||
); | ||
|
||
await serverAct(() => B.resolve()); | ||
assertLog(['B']); | ||
|
||
expect(getVisibleChildren(container)).toEqual( | ||
<div> | ||
<span>A</span> | ||
<span>B</span> | ||
<span>C</span> | ||
</div>, | ||
); | ||
}); | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a difference between a default
SuspenseList
and just wrapping it with aFragment
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think so but I always forget the special cases for updates that affect nested SuspenseList.
I forget that the default doesn't do anything a lot but basically you probably always should pick some options. We could make it a required props. It's mainly useful to disable conditionally. However, we don't really an option other than
undefined
. I've been thinking that maybe should add"independent"
as an option for that case.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This case is a bit interesting:
react/packages/react-reconciler/src/__tests__/ReactSuspenseList-test.js
Lines 578 to 585 in a3abf5f
It's a case where because the outer one is in
"together"
mode, it blocks the inner ones from revealing independently. That's the same as if it was a Fragment so I think it's the same.However, if the inner one had an explicit
"independent"
string, shouldn't that override the outer"together"
mode?