-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Feature Flag for Selective Hydration * Enable Synchronous Hydration of Discrete Events * Resolve cyclic dependency
- Loading branch information
1 parent
4bb0e96
commit 3694a3b
Showing
12 changed files
with
202 additions
and
16 deletions.
There are no files selected for viewing
117 changes: 117 additions & 0 deletions
117
packages/react-dom/src/__tests__/ReactDOMServerSelectiveHydration-test.internal.js
This file contains 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,117 @@ | ||
/** | ||
* 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 | ||
*/ | ||
|
||
'use strict'; | ||
|
||
let React; | ||
let ReactDOM; | ||
let ReactDOMServer; | ||
let Scheduler; | ||
let ReactFeatureFlags; | ||
let Suspense; | ||
|
||
function dispatchClickEvent(target) { | ||
const mouseOutEvent = document.createEvent('MouseEvents'); | ||
mouseOutEvent.initMouseEvent( | ||
'click', | ||
true, | ||
true, | ||
window, | ||
0, | ||
50, | ||
50, | ||
50, | ||
50, | ||
false, | ||
false, | ||
false, | ||
false, | ||
0, | ||
target, | ||
); | ||
return target.dispatchEvent(mouseOutEvent); | ||
} | ||
|
||
describe('ReactDOMServerSelectiveHydration', () => { | ||
beforeEach(() => { | ||
jest.resetModuleRegistry(); | ||
|
||
ReactFeatureFlags = require('shared/ReactFeatureFlags'); | ||
ReactFeatureFlags.enableSuspenseServerRenderer = true; | ||
ReactFeatureFlags.enableSelectiveHydration = true; | ||
|
||
React = require('react'); | ||
ReactDOM = require('react-dom'); | ||
ReactDOMServer = require('react-dom/server'); | ||
Scheduler = require('scheduler'); | ||
Suspense = React.Suspense; | ||
}); | ||
|
||
it('hydrates the target boundary synchronously during a click', async () => { | ||
function Child({text}) { | ||
Scheduler.unstable_yieldValue(text); | ||
return ( | ||
<span | ||
onClick={e => { | ||
e.preventDefault(); | ||
Scheduler.unstable_yieldValue('Clicked ' + text); | ||
}}> | ||
{text} | ||
</span> | ||
); | ||
} | ||
|
||
function App() { | ||
Scheduler.unstable_yieldValue('App'); | ||
return ( | ||
<div> | ||
<Suspense fallback="Loading..."> | ||
<Child text="A" /> | ||
</Suspense> | ||
<Suspense fallback="Loading..."> | ||
<Child text="B" /> | ||
</Suspense> | ||
</div> | ||
); | ||
} | ||
|
||
let finalHTML = ReactDOMServer.renderToString(<App />); | ||
|
||
expect(Scheduler).toHaveYielded(['App', 'A', 'B']); | ||
|
||
let container = document.createElement('div'); | ||
// We need this to be in the document since we'll dispatch events on it. | ||
document.body.appendChild(container); | ||
|
||
container.innerHTML = finalHTML; | ||
|
||
let span = container.getElementsByTagName('span')[1]; | ||
|
||
let root = ReactDOM.unstable_createRoot(container, {hydrate: true}); | ||
root.render(<App />); | ||
|
||
// Nothing has been hydrated so far. | ||
expect(Scheduler).toHaveYielded([]); | ||
|
||
// This should synchronously hydrate the root App and the second suspense | ||
// boundary. | ||
let result = dispatchClickEvent(span); | ||
|
||
// The event should have been canceled because we called preventDefault. | ||
expect(result).toBe(false); | ||
|
||
// We rendered App, B and then invoked the event without rendering A. | ||
expect(Scheduler).toHaveYielded(['App', 'B', 'Clicked B']); | ||
|
||
// After continuing the scheduler, we finally hydrate A. | ||
expect(Scheduler).toFlushAndYield(['A']); | ||
|
||
document.body.removeChild(container); | ||
}); | ||
}); |
This file contains 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 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 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 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 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 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 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 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 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 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 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