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

fix(hydration): Suspender returns Fragment with multiple DOM-nodes #4438

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
69 changes: 69 additions & 0 deletions compat/test/browser/suspense-hydration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,75 @@ describe('suspense hydration', () => {
});
});

it('Should hydrate a fragment with multiple children correctly', () => {
scratch.innerHTML = '<div>Hello</div><div>World!</div>';
clearLog();

const [Lazy, resolve] = createLazy();
hydrate(
<Suspense>
<Lazy />
</Suspense>,
scratch
);
rerender(); // Flush rerender queue to mimic what preact will really do
expect(scratch.innerHTML).to.equal('<div>Hello</div><div>World!</div>');
expect(getLog()).to.deep.equal([]);
clearLog();

return resolve(() => (
<>
<div>Hello</div>
<div>World!</div>
</>
)).then(() => {
rerender();
expect(scratch.innerHTML).to.equal('<div>Hello</div><div>World!</div>');
expect(getLog()).to.deep.equal([]);

clearLog();
});
});

// This is an expected fail case, we should encourage folks to not have adjacent
// nodes of the same type, or return a single wrapping dom node from a lazy boundary.
it.skip('Should hydrate a fragment with multiple children and an adjacent node correctly', () => {
scratch.innerHTML = '<div>Hello</div><div>World!</div><div>Baz</div>';
clearLog();

const [Lazy, resolve] = createLazy();
hydrate(
<>
<Suspense>
<Lazy />
</Suspense>
<div>Baz</div>
</>,
scratch
);
rerender(); // Flush rerender queue to mimic what preact will really do
expect(scratch.innerHTML).to.equal(
'<div>Hello</div><div>World!</div><div>Baz</div>'
);
expect(getLog()).to.deep.equal([]);
clearLog();

return resolve(() => (
<>
<div>Hello</div>
<div>World!</div>
</>
)).then(() => {
rerender();
expect(scratch.innerHTML).to.equal(
'<div>Hello</div><div>World!</div><div>Baz</div>'
);
expect(getLog()).to.deep.equal([]);

clearLog();
});
});

it('should allow siblings to update around suspense boundary', () => {
scratch.innerHTML = '<div>Count: 0</div><div>Hello</div>';
clearLog();
Expand Down
1 change: 1 addition & 0 deletions jsx-runtime/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ function createVNode(type, props, key, isStaticChildren, __source, __self) {
_nextDom: undefined,
_component: null,
constructor: undefined,
_excess: null,
_original: --vnodeId,
_index: -1,
_flags: 0,
Expand Down
1 change: 1 addition & 0 deletions mangle.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"props": {
"$_listeners": "l",
"$_cleanup": "__c",
"$_excess": "__x",
"$__hooks": "__H",
"$_list": "__",
"$_pendingEffects": "__h",
Expand Down
1 change: 1 addition & 0 deletions src/create-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export function createVNode(type, props, key, ref, original) {
props,
key,
ref,
_excess: null,
_children: null,
_parent: null,
_depth: 0,
Expand Down
3 changes: 2 additions & 1 deletion src/diff/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export function diff(
if (oldVNode._flags & MODE_SUSPENDED) {
isHydrating = !!(oldVNode._flags & MODE_HYDRATE);
oldDom = newVNode._dom = oldVNode._dom;
excessDomChildren = [oldDom];
excessDomChildren = oldVNode._excess;
}

if ((tmp = options._diff)) tmp(newVNode);
Expand Down Expand Up @@ -277,6 +277,7 @@ export function diff(
newVNode._flags |= isHydrating
? MODE_HYDRATE | MODE_SUSPENDED
: MODE_HYDRATE;
newVNode._excess = [...excessDomChildren];
Copy link
Member Author

@JoviDeCroock JoviDeCroock Jul 8, 2024

Choose a reason for hiding this comment

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

An alternative to copying here would be to mark certain nodes i.e. we mark it with the oldVNode signature and then when we resume hydration in the initial src/diff/index.js check we unlock it by checking. Or we start marking the DOM-node as consumed so we rely less on these null entries

excessDomChildren[excessDomChildren.indexOf(oldDom)] = null;
// ^ could possibly be simplified to:
// excessDomChildren.length = 0;
Expand Down
1 change: 1 addition & 0 deletions src/internal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ declare global {
_children: Array<VNode<any>> | null;
_parent: VNode | null;
_depth: number | null;
_excess: Array<any> | null;
/**
* The [first (for Fragments)] DOM child of a VNode
*/
Expand Down
Loading