Skip to content

Commit 913af4c

Browse files
committed
fix(hydration): handle v-if node mismatch
1 parent 22f359b commit 913af4c

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2299,6 +2299,16 @@ describe('SSR hydration', () => {
22992299
expect(`Hydration node mismatch`).not.toHaveBeenWarned()
23002300
})
23012301

2302+
test('comment mismatch (v-if)', () => {
2303+
const { container } = mountWithHydration(`<!--v-if-->`, () =>
2304+
h('div', { 'data-allow-mismatch': '' }, [h('span', 'value')]),
2305+
)
2306+
expect(container.innerHTML).toBe(
2307+
'<div data-allow-mismatch=""><span>value</span></div>',
2308+
)
2309+
expect(`Hydration node mismatch`).not.toHaveBeenWarned()
2310+
})
2311+
23022312
test('comment mismatch (text)', () => {
23032313
const { container } = mountWithHydration(
23042314
`<div data-allow-mismatch="children">foobar</div>`,

packages/runtime-core/src/hydration.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,10 @@ export function createHydrationFunctions(
675675
slotScopeIds: string[] | null,
676676
isFragment: boolean,
677677
): Node | null => {
678-
if (!isMismatchAllowed(node.parentElement!, MismatchTypes.CHILDREN)) {
678+
if (
679+
!isMismatchAllowed(node.parentElement!, MismatchTypes.CHILDREN) &&
680+
!isCommentNodeMismatch(node, vnode)
681+
) {
679682
;(__DEV__ || __FEATURE_PROD_HYDRATION_MISMATCH_DETAILS__) &&
680683
warn(
681684
`Hydration node mismatch:\n- rendered on server:`,
@@ -993,3 +996,12 @@ function isMismatchAllowed(
993996
return allowedAttr.split(',').includes(MismatchTypeString[allowedType])
994997
}
995998
}
999+
1000+
// data-allow-mismatch + v-if
1001+
function isCommentNodeMismatch(node: Node, vnode: VNode): boolean {
1002+
if (node.nodeType !== DOMNodeTypes.COMMENT || !vnode.props) {
1003+
return false
1004+
}
1005+
1006+
return allowMismatchAttr in vnode.props
1007+
}

0 commit comments

Comments
 (0)