Skip to content
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

Partial Hydration #14717

Merged
merged 21 commits into from
Feb 12, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
bc3e2f9
Basic partial hydration test
sebmarkbage Jan 18, 2019
433e6e5
Render comments around Suspense components
sebmarkbage Jan 19, 2019
2d28a52
Add DehydratedSuspenseComponent type of work
sebmarkbage Jan 24, 2019
f06a540
Add comment node as hydratable instance type as placeholder for suspense
sebmarkbage Jan 24, 2019
97a6664
Skip past nodes within the Suspense boundary
sebmarkbage Jan 28, 2019
1b3e0a2
A dehydrated suspense boundary comment should be considered a sibling
sebmarkbage Jan 28, 2019
6febcae
Retry hydrating at offscreen pri or after ping if suspended
sebmarkbage Jan 28, 2019
7af0b8a
Enter hydration state when retrying dehydrated suspense boundary
sebmarkbage Jan 28, 2019
dac0688
Delete all children within a dehydrated suspense boundary when it's d…
sebmarkbage Jan 28, 2019
92fb62a
Delete server rendered content when props change before hydration com…
sebmarkbage Jan 29, 2019
6ae914c
Make test internal
sebmarkbage Jan 29, 2019
e144a5e
Wrap in act
sebmarkbage Feb 10, 2019
eb3ea2d
Change SSR Fixture to use Partial Hydration
sebmarkbage Feb 9, 2019
97eb545
Changes to any parent Context forces clearing dehydrated content
sebmarkbage Feb 10, 2019
c91092b
Wrap in feature flag
sebmarkbage Feb 11, 2019
2e16dc1
Treat Suspense boundaries without fallbacks as if not-boundaries
sebmarkbage Feb 12, 2019
1db9dd2
Fix clearing of nested suspense boundaries
sebmarkbage Feb 12, 2019
3a89f65
ping -> retry
acdlite Feb 12, 2019
e0e1ded
Typo
acdlite Feb 12, 2019
ca2d628
Use didReceiveUpdate instead of manually comparing props
sebmarkbage Feb 12, 2019
34a132c
Leave comment for why it's ok to ignore the timeout
sebmarkbage Feb 12, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Delete all children within a dehydrated suspense boundary when it's d…
…eleted
  • Loading branch information
sebmarkbage committed Feb 9, 2019
commit dac06884324eaad8b10c03bf87ef9b20d2b53284
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,57 @@ describe('ReactDOMServerPartialHydration', () => {
expect(container.firstChild.firstChild.tagName).toBe('DIV');
expect(container.firstChild.firstChild.textContent).toBe('First');
});

it('can delete the dehydrated boundary before it is hydrated', () => {
let suspend = false;
let promise = new Promise(() => {});
let hideMiddle;

function Child() {
if (suspend) {
throw promise;
} else {
return (
<React.Fragment>
<div>Middle</div>
Some text
</React.Fragment>
);
}
}

function App() {
let [visible, setVisibilty] = React.useState(true);
hideMiddle = () => setVisibilty(false);

return (
<div>
<div>Before</div>
{visible ? (
<Suspense fallback="Loading...">
<Child />
</Suspense>
) : null}
<div>After</div>
</div>
);
}

suspend = false;
let finalHTML = ReactDOMServer.renderToString(<App />);
let container = document.createElement('div');
container.innerHTML = finalHTML;

// On the client we don't have all data yet but we want to start
// hydrating anyway.
suspend = true;
ReactDOM.hydrate(<App />, container);

expect(container.firstChild.children[1].textContent).toBe('Middle');

// In this state, we can still delete the boundary.
hideMiddle();

expect(container.firstChild.children[1].textContent).toBe('After');
});
});
43 changes: 43 additions & 0 deletions packages/react-dom/src/client/ReactDOMHostConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,49 @@ export function removeChildFromContainer(
}
}

export function clearSuspenseBoundary(
parentInstance: Instance,
suspenseInstance: SuspenseInstance,
): void {
let node = suspenseInstance;
// Delete all nodes within this suspense boundary.
// There might be nested nodes so we need to keep track of how
// deep we are and only break out when we're back on top.
let depth = 0;
do {
let nextNode = node.nextSibling;
parentInstance.removeChild(node);
if (nextNode && nextNode.nodeType === COMMENT_NODE) {
let data = ((nextNode: any).data: string);
if (data === SUSPENSE_END_DATA) {
if (depth === 0) {
parentInstance.removeChild(nextNode);
return;
} else {
depth--;
}
} else if (nextNode === SUSPENSE_START_DATA) {
sebmarkbage marked this conversation as resolved.
Show resolved Hide resolved
depth++;
}
}
node = nextNode;
} while (node);
// TODO: Warn, we didn't find the end comment boundary.
}

export function clearSuspenseBoundaryFromContainer(
container: Container,
suspenseInstance: SuspenseInstance,
): void {
if (container.nodeType === COMMENT_NODE) {
clearSuspenseBoundary((container.parentNode: any), suspenseInstance);
} else if (container.nodeType === ELEMENT_NODE) {
clearSuspenseBoundary((container: any), suspenseInstance);
} else {
// Document nodes should never contain suspense boundaries.
}
}

export function hideInstance(instance: Instance): void {
// TODO: Does this work for all element types? What about MathML? Should we
// pass host context to this method?
Expand Down
26 changes: 24 additions & 2 deletions packages/react-reconciler/src/ReactFiberCommitWork.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import type {
Instance,
TextInstance,
SuspenseInstance,
Container,
ChildSet,
UpdatePayload,
Expand Down Expand Up @@ -80,6 +81,8 @@ import {
insertInContainerBefore,
removeChild,
removeChildFromContainer,
clearSuspenseBoundary,
clearSuspenseBoundaryFromContainer,
replaceContainerChildren,
createContainerChildSet,
hideInstance,
Expand Down Expand Up @@ -1037,11 +1040,30 @@ function unmountHostComponents(current): void {
// After all the children have unmounted, it is now safe to remove the
// node from the tree.
if (currentParentIsContainer) {
removeChildFromContainer((currentParent: any), node.stateNode);
removeChildFromContainer(
((currentParent: any): Container),
(node.stateNode: Instance | TextInstance),
);
} else {
removeChild((currentParent: any), node.stateNode);
removeChild(
((currentParent: any): Instance),
(node.stateNode: Instance | TextInstance),
);
}
// Don't visit children because we already visited them.
} else if (node.tag === DehydratedSuspenseComponent) {
// Delete the dehydrated suspense boundary and all of its content.
if (currentParentIsContainer) {
clearSuspenseBoundaryFromContainer(
((currentParent: any): Container),
(node.stateNode: SuspenseInstance),
);
} else {
clearSuspenseBoundary(
((currentParent: any): Instance),
(node.stateNode: SuspenseInstance),
);
}
} else if (node.tag === HostPortal) {
// When we go into a portal, it becomes the parent to remove from.
// We will reassign it back when we pop the portal on the way up.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export opaque type Props = mixed; // eslint-disable-line no-undef
export opaque type Container = mixed; // eslint-disable-line no-undef
export opaque type Instance = mixed; // eslint-disable-line no-undef
export opaque type TextInstance = mixed; // eslint-disable-line no-undef
export opaque type SuspenseInstance = mixed; // eslint-disable-line no-undef
export opaque type HydratableInstance = mixed; // eslint-disable-line no-undef
export opaque type PublicInstance = mixed; // eslint-disable-line no-undef
export opaque type HostContext = mixed; // eslint-disable-line no-undef
Expand Down Expand Up @@ -112,6 +113,9 @@ export const hydrateInstance = $$$hostConfig.hydrateInstance;
export const hydrateTextInstance = $$$hostConfig.hydrateTextInstance;
export const getNextHydratableInstanceAfterSuspenseInstance =
$$$hostConfig.getNextHydratableInstanceAfterSuspenseInstance;
export const clearSuspenseBoundary = $$$hostConfig.clearSuspenseBoundary;
export const clearSuspenseBoundaryFromContainer =
$$$hostConfig.clearSuspenseBoundaryFromContainer;
export const didNotMatchHydratedContainerTextInstance =
$$$hostConfig.didNotMatchHydratedContainerTextInstance;
export const didNotMatchHydratedTextInstance =
Expand Down
3 changes: 3 additions & 0 deletions packages/shared/HostConfigWithNoHydration.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ function shim(...args: any) {
}

// Hydration (when unsupported)
export type SuspenseInstance = mixed;
export const supportsHydration = false;
export const canHydrateInstance = shim;
export const canHydrateTextInstance = shim;
Expand All @@ -31,6 +32,8 @@ export const getFirstHydratableChild = shim;
export const hydrateInstance = shim;
export const hydrateTextInstance = shim;
export const getNextHydratableInstanceAfterSuspenseInstance = shim;
export const clearSuspenseBoundary = shim;
export const clearSuspenseBoundaryFromContainer = shim;
export const didNotMatchHydratedContainerTextInstance = shim;
export const didNotMatchHydratedTextInstance = shim;
export const didNotHydrateContainerInstance = shim;
Expand Down