-
Notifications
You must be signed in to change notification settings - Fork 49k
Allow suspending in the shell during hydration #23304
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
2 commits
Select commit
Hold shift + click to select a range
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
216 changes: 216 additions & 0 deletions
216
packages/react-dom/src/__tests__/ReactDOMFizzShellHydration-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,216 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its 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 | ||
*/ | ||
|
||
let JSDOM; | ||
let React; | ||
let ReactDOM; | ||
let Scheduler; | ||
let clientAct; | ||
let ReactDOMFizzServer; | ||
let Stream; | ||
let document; | ||
let writable; | ||
let container; | ||
let buffer = ''; | ||
let hasErrored = false; | ||
let fatalError = undefined; | ||
let textCache; | ||
|
||
describe('ReactDOMFizzShellHydration', () => { | ||
beforeEach(() => { | ||
jest.resetModules(); | ||
JSDOM = require('jsdom').JSDOM; | ||
React = require('react'); | ||
ReactDOM = require('react-dom'); | ||
Scheduler = require('scheduler'); | ||
clientAct = require('jest-react').act; | ||
ReactDOMFizzServer = require('react-dom/server'); | ||
Stream = require('stream'); | ||
|
||
textCache = new Map(); | ||
|
||
// 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'); | ||
|
||
buffer = ''; | ||
hasErrored = false; | ||
|
||
writable = new Stream.PassThrough(); | ||
writable.setEncoding('utf8'); | ||
writable.on('data', chunk => { | ||
buffer += chunk; | ||
}); | ||
writable.on('error', error => { | ||
hasErrored = true; | ||
fatalError = error; | ||
}); | ||
}); | ||
|
||
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 fakeBody = document.createElement('body'); | ||
fakeBody.innerHTML = bufferedContent; | ||
while (fakeBody.firstChild) { | ||
const node = fakeBody.firstChild; | ||
if (node.nodeName === 'SCRIPT') { | ||
const script = document.createElement('script'); | ||
script.textContent = node.textContent; | ||
fakeBody.removeChild(node); | ||
container.appendChild(script); | ||
} else { | ||
container.appendChild(node); | ||
} | ||
} | ||
} | ||
|
||
function resolveText(text) { | ||
const record = textCache.get(text); | ||
if (record === undefined) { | ||
const newRecord = { | ||
status: 'resolved', | ||
value: text, | ||
}; | ||
textCache.set(text, newRecord); | ||
} else if (record.status === 'pending') { | ||
const thenable = record.value; | ||
record.status = 'resolved'; | ||
record.value = text; | ||
thenable.pings.forEach(t => t()); | ||
} | ||
} | ||
|
||
function readText(text) { | ||
const record = textCache.get(text); | ||
if (record !== undefined) { | ||
switch (record.status) { | ||
case 'pending': | ||
throw record.value; | ||
case 'rejected': | ||
throw record.value; | ||
case 'resolved': | ||
return record.value; | ||
} | ||
} else { | ||
Scheduler.unstable_yieldValue(`Suspend! [${text}]`); | ||
|
||
const thenable = { | ||
pings: [], | ||
then(resolve) { | ||
if (newRecord.status === 'pending') { | ||
thenable.pings.push(resolve); | ||
} else { | ||
Promise.resolve().then(() => resolve(newRecord.value)); | ||
} | ||
}, | ||
}; | ||
|
||
const newRecord = { | ||
status: 'pending', | ||
value: thenable, | ||
}; | ||
textCache.set(text, newRecord); | ||
|
||
throw thenable; | ||
} | ||
} | ||
|
||
// function Text({text}) { | ||
// Scheduler.unstable_yieldValue(text); | ||
// return text; | ||
// } | ||
|
||
function AsyncText({text}) { | ||
readText(text); | ||
Scheduler.unstable_yieldValue(text); | ||
return text; | ||
} | ||
|
||
function resetTextCache() { | ||
textCache = new Map(); | ||
} | ||
|
||
test('suspending in the shell during hydration', async () => { | ||
const div = React.createRef(null); | ||
|
||
function App() { | ||
return ( | ||
<div ref={div}> | ||
<AsyncText text="Shell" /> | ||
</div> | ||
); | ||
} | ||
|
||
// Server render | ||
await resolveText('Shell'); | ||
await serverAct(async () => { | ||
const {pipe} = ReactDOMFizzServer.renderToPipeableStream(<App />); | ||
pipe(writable); | ||
}); | ||
expect(Scheduler).toHaveYielded(['Shell']); | ||
const dehydratedDiv = container.getElementsByTagName('div')[0]; | ||
|
||
// Clear the cache and start rendering on the client | ||
resetTextCache(); | ||
|
||
// Hydration suspends because the data for the shell hasn't loaded yet | ||
await clientAct(async () => { | ||
ReactDOM.hydrateRoot(container, <App />); | ||
}); | ||
expect(Scheduler).toHaveYielded(['Suspend! [Shell]']); | ||
expect(div.current).toBe(null); | ||
expect(container.textContent).toBe('Shell'); | ||
|
||
// The shell loads and hydration finishes | ||
await clientAct(async () => { | ||
await resolveText('Shell'); | ||
}); | ||
expect(Scheduler).toHaveYielded(['Shell']); | ||
expect(div.current).toBe(dehydratedDiv); | ||
expect(container.textContent).toBe('Shell'); | ||
}); | ||
|
||
test('suspending in the shell during a normal client render', async () => { | ||
// Same as previous test but during a normal client render, no hydration | ||
function App() { | ||
return <AsyncText text="Shell" />; | ||
} | ||
|
||
const root = ReactDOM.createRoot(container); | ||
await clientAct(async () => { | ||
root.render(<App />); | ||
}); | ||
expect(Scheduler).toHaveYielded(['Suspend! [Shell]']); | ||
|
||
await clientAct(async () => { | ||
await resolveText('Shell'); | ||
}); | ||
expect(Scheduler).toHaveYielded(['Shell']); | ||
expect(container.textContent).toBe('Shell'); | ||
}); | ||
}); |
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
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
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 |
---|---|---|
|
@@ -389,17 +389,6 @@ describe('ReactSuspense', () => { | |
expect(root).toMatchRenderedOutput('Hi'); | ||
}); | ||
|
||
it('throws if tree suspends and none of the Suspense ancestors have a boundary', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Deleted this test because it was a duplicate of the one in ReactSuspenseWithNoopRenderer |
||
ReactTestRenderer.create(<AsyncText text="Hi" ms={1000} />, { | ||
unstable_isConcurrent: true, | ||
}); | ||
|
||
expect(Scheduler).toFlushAndThrow( | ||
'AsyncText suspended while rendering, but no fallback UI was specified.', | ||
); | ||
expect(Scheduler).toHaveYielded(['Suspend! [Hi]', 'Suspend! [Hi]']); | ||
}); | ||
|
||
it('updates memoized child of suspense component when context updates (simple memo)', () => { | ||
const {useContext, createContext, useState, memo} = React; | ||
|
||
|
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
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.
TIL