-
Notifications
You must be signed in to change notification settings - Fork 47k
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
Selective Hydration #16880
Selective Hydration #16880
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,7 +27,12 @@ import { | |
findCurrentHostFiberWithNoPortals, | ||
} from 'react-reconciler/reflection'; | ||
import {get as getInstance} from 'shared/ReactInstanceMap'; | ||
import {HostComponent, ClassComponent} from 'shared/ReactWorkTags'; | ||
import { | ||
HostComponent, | ||
ClassComponent, | ||
HostRoot, | ||
SuspenseComponent, | ||
} from 'shared/ReactWorkTags'; | ||
import getComponentName from 'shared/getComponentName'; | ||
import invariant from 'shared/invariant'; | ||
import warningWithoutStack from 'shared/warningWithoutStack'; | ||
|
@@ -362,6 +367,21 @@ export function getPublicRootInstance( | |
} | ||
} | ||
|
||
export function attemptSynchronousHydration(fiber: Fiber): void { | ||
switch (fiber.tag) { | ||
case HostRoot: | ||
let root: FiberRoot = fiber.stateNode; | ||
if (root.hydrate) { | ||
// Flush the first scheduled "update". | ||
flushRoot(root, root.firstPendingTime); | ||
} | ||
break; | ||
case SuspenseComponent: | ||
flushSync(() => scheduleWork(fiber, Sync)); | ||
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. I find the methods that automatically flush at the end confusing unless they are at the bottom of the stack. In the work loop I've started to prefer using two separate calls, one to queue the sync work and a second explicit call that flushes it. 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. Yea I also don't like the closure. I would've done it that way but didn't feel justified to move this whole function into WorkLoop. Although the split between WorkLoop and Reconciler is a bit arbitrary right now. Reconciler is effectively just a smaller set of exposed functions from WorkLoop. |
||
break; | ||
} | ||
} | ||
|
||
export {findHostInstance}; | ||
|
||
export {findHostInstanceWithWarning}; | ||
|
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.
Yeah this is kind of weird but it makes sense :D
I suppose the other way to model this is, when hydrating, immediately commit the root in a dehydrated state.