Skip to content

Commit 82a7bb9

Browse files
fix(runtime): make isSameVnode return false on initial render in a hydration case (#5891)
* fix(runtime): make isSameVnode return false on initial render in a hydration case * prettier
1 parent a7c212c commit 82a7bb9

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { isSameVnode } from '../vdom-render';
2+
3+
describe('isSameVnode', () => {
4+
it('should detect objectively same nodes', () => {
5+
const vnode1: any = {
6+
$tag$: 'div',
7+
$key$: '1',
8+
$elm$: { nodeType: 9 },
9+
};
10+
const vnode2: any = {
11+
$tag$: 'div',
12+
$key$: '1',
13+
$elm$: { nodeType: 9 },
14+
};
15+
expect(isSameVnode(vnode1, vnode2)).toBe(true);
16+
});
17+
18+
it('should return false in case of hyration', () => {
19+
const vnode1: any = {
20+
$tag$: 'slot',
21+
$key$: '1',
22+
$elm$: { nodeType: 9 },
23+
$nodeId$: 1,
24+
};
25+
const vnode2: any = {
26+
$tag$: 'slot',
27+
$key$: '1',
28+
$elm$: { nodeType: 9 },
29+
};
30+
const vnode3: any = {
31+
$tag$: 'slot',
32+
$key$: '1',
33+
$elm$: { nodeType: 8 },
34+
$nodeId$: 2,
35+
};
36+
expect(isSameVnode(vnode1, vnode2, true)).toBe(false);
37+
expect(isSameVnode(vnode3, vnode2, true)).toBe(true);
38+
});
39+
});

src/runtime/vdom/vdom-render.ts

+13
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,19 @@ export const isSameVnode = (leftVNode: d.VNode, rightVNode: d.VNode, isInitialRe
610610
// need to have the same element tag, and same key to be the same
611611
if (leftVNode.$tag$ === rightVNode.$tag$) {
612612
if (BUILD.slotRelocation && leftVNode.$tag$ === 'slot') {
613+
// We are not considering the same node if:
614+
if (
615+
// The component gets hydrated and no VDOM has been initialized.
616+
// Here the comparison can't happen as $name$ property is not set for `leftNode`.
617+
'$nodeId$' in leftVNode &&
618+
isInitialRender &&
619+
// `leftNode` is not from type HTMLComment which would cause many
620+
// hydration comments to be removed
621+
leftVNode.$elm$.nodeType !== 8
622+
) {
623+
return false;
624+
}
625+
613626
return leftVNode.$name$ === rightVNode.$name$;
614627
}
615628
// this will be set if JSX tags in the build have `key` attrs set on them

0 commit comments

Comments
 (0)