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

Experimental support for resuming suspended hydration #2214

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
2 changes: 2 additions & 0 deletions mangle.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
"$_children": "__k",
"$_suspensions": "__u",
"$_dom": "__e",
"$_hydrateDom": "__s",
"$_hydrating": "__h",
"$_component": "__c",
"$__html": "__html",
"$_parent": "__",
Expand Down
2 changes: 2 additions & 0 deletions src/create-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ export function createVNode(type, props, key, ref) {
_dom: null,
_lastDomChild: null,
_component: null,
_hydrateDom: null,
_hydrating: false,
constructor: undefined
};

Expand Down
15 changes: 15 additions & 0 deletions src/diff/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ export function diff(
// constructor as undefined. This to prevent JSON-injection.
if (newVNode.constructor !== undefined) return null;

// If this was the innermost VNode at a point where the tree suspended,
// pick up diffing where we left off using the saved DOM element and hydration state.
if (oldVNode._hydrateDom && excessDomChildren == null) {
oldDom = oldVNode._hydrateDom;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
oldDom = oldVNode._hydrateDom;
newVNode._dom = oldDom = oldVNode._hydrateDom;

excessDomChildren = [oldDom];
newVNode._dom = oldDom;
oldVNode._hydrateDom = null;
isHydrating = oldVNode._hydrating;
}

if ((tmp = options._diff)) tmp(newVNode);

try {
Expand Down Expand Up @@ -215,6 +225,11 @@ export function diff(

if ((tmp = options.diffed)) tmp(newVNode);
} catch (e) {
// Before bailing out, mark the current VNode with the DOM element and hydration state.
// We can use this information if we return here to render later on.
newVNode._dom = oldDom;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be reduced to:

Suggested change
newVNode._dom = oldDom;
oldVNode._hydrateDom = newVNode._dom = oldDom;

This removes the need for the next line (multi-line suggestions aren't available)

oldVNode._hydrateDom = oldDom;
oldVNode._hydrating = isHydrating;
options._catchError(e, newVNode, oldVNode);
}

Expand Down