Skip to content

Commit 164af63

Browse files
authored
fix(hydration): respect data-allow-mismatch on conditional branches (#12801)
close #12782
1 parent 2f374cd commit 164af63

2 files changed

Lines changed: 77 additions & 2 deletions

File tree

‎packages/runtime-core/__tests__/hydration.spec.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2636,6 +2636,46 @@ describe('SSR hydration', () => {
26362636
expect(`Hydration node mismatch`).not.toHaveBeenWarned()
26372637
})
26382638

2639+
test('comment mismatch (v-if)', () => {
2640+
const { container } = mountWithHydration(`<!--v-if-->`, () =>
2641+
h('div', { 'data-allow-mismatch': '' }, [h('span', 'value')]),
2642+
)
2643+
expect(container.innerHTML).toBe(
2644+
'<div data-allow-mismatch=""><span>value</span></div>',
2645+
)
2646+
expect(`Hydration node mismatch`).not.toHaveBeenWarned()
2647+
})
2648+
2649+
test('comment mismatch (v-if branch removed)', () => {
2650+
const { container } = mountWithHydration(
2651+
`<div data-allow-mismatch=""><span>value</span></div>`,
2652+
() => createCommentVNode('v-if', true),
2653+
)
2654+
expect(container.innerHTML).toBe('<!--v-if-->')
2655+
expect(`Hydration node mismatch`).not.toHaveBeenWarned()
2656+
})
2657+
2658+
test('node mismatch (v-else branches)', () => {
2659+
const { container } = mountWithHydration(
2660+
`<span data-allow-mismatch="">server</span>`,
2661+
() => h('div', { 'data-allow-mismatch': '' }, 'client'),
2662+
)
2663+
expect(container.innerHTML).toBe(
2664+
'<div data-allow-mismatch="">client</div>',
2665+
)
2666+
expect(`Hydration node mismatch`).not.toHaveBeenWarned()
2667+
})
2668+
2669+
test('comment mismatch (v-if) only allows children mismatches', () => {
2670+
const { container } = mountWithHydration(`<!--v-if-->`, () =>
2671+
h('div', { 'data-allow-mismatch': 'class' }, [h('span', 'value')]),
2672+
)
2673+
expect(container.innerHTML).toBe(
2674+
'<div data-allow-mismatch="class"><span>value</span></div>',
2675+
)
2676+
expect(`Hydration node mismatch`).toHaveBeenWarned()
2677+
})
2678+
26392679
test('comment mismatch (text)', () => {
26402680
const { container } = mountWithHydration(
26412681
`<div data-allow-mismatch="children">foobar</div>`,

‎packages/runtime-core/src/hydration.ts‎

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ export function createHydrationFunctions(
691691
slotScopeIds: string[] | null,
692692
isFragment: boolean,
693693
): Node | null => {
694-
if (!isMismatchAllowed(node.parentElement!, MismatchTypes.CHILDREN)) {
694+
if (!isNodeMismatchAllowed(node, vnode)) {
695695
;(__DEV__ || __FEATURE_PROD_HYDRATION_MISMATCH_DETAILS__) &&
696696
warn(
697697
`Hydration node mismatch:\n- rendered on server:`,
@@ -998,7 +998,16 @@ function isMismatchAllowed(
998998
el = el.parentElement
999999
}
10001000
}
1001-
const allowedAttr = el && el.getAttribute(allowMismatchAttr)
1001+
return isMismatchAllowedByAttr(
1002+
el && el.getAttribute(allowMismatchAttr),
1003+
allowedType,
1004+
)
1005+
}
1006+
1007+
function isMismatchAllowedByAttr(
1008+
allowedAttr: string | null,
1009+
allowedType: MismatchTypes,
1010+
): boolean {
10021011
if (allowedAttr == null) {
10031012
return false
10041013
} else if (allowedAttr === '') {
@@ -1012,3 +1021,29 @@ function isMismatchAllowed(
10121021
return list.includes(MismatchTypeString[allowedType])
10131022
}
10141023
}
1024+
1025+
function isNodeMismatchAllowed(node: Node, vnode: VNode): boolean {
1026+
return (
1027+
isMismatchAllowed(node.parentElement, MismatchTypes.CHILDREN) ||
1028+
isMismatchAllowedByNode(node) ||
1029+
isMismatchAllowedByVNode(vnode)
1030+
)
1031+
}
1032+
1033+
function isMismatchAllowedByNode(node: Node): boolean {
1034+
return (
1035+
node.nodeType === DOMNodeTypes.ELEMENT &&
1036+
isMismatchAllowedByAttr(
1037+
(node as Element).getAttribute(allowMismatchAttr),
1038+
MismatchTypes.CHILDREN,
1039+
)
1040+
)
1041+
}
1042+
1043+
function isMismatchAllowedByVNode({ props }: VNode): boolean {
1044+
const allowedAttr = props && props[allowMismatchAttr]
1045+
return (
1046+
typeof allowedAttr === 'string' &&
1047+
isMismatchAllowedByAttr(allowedAttr, MismatchTypes.CHILDREN)
1048+
)
1049+
}

0 commit comments

Comments
 (0)