From: daiwei Date: Sat, 1 Feb 2025 03:28:21 +0000 (+0800) Subject: fix(hydration): handle v-if node mismatch X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=913af4c5438556988cd24ce3b0df8b74a1ab9c73;p=thirdparty%2Fvuejs%2Fcore.git fix(hydration): handle v-if node mismatch --- diff --git a/packages/runtime-core/__tests__/hydration.spec.ts b/packages/runtime-core/__tests__/hydration.spec.ts index 56011d0635..8daf68c27c 100644 --- a/packages/runtime-core/__tests__/hydration.spec.ts +++ b/packages/runtime-core/__tests__/hydration.spec.ts @@ -2299,6 +2299,16 @@ describe('SSR hydration', () => { expect(`Hydration node mismatch`).not.toHaveBeenWarned() }) + test('comment mismatch (v-if)', () => { + const { container } = mountWithHydration(``, () => + h('div', { 'data-allow-mismatch': '' }, [h('span', 'value')]), + ) + expect(container.innerHTML).toBe( + '
value
', + ) + expect(`Hydration node mismatch`).not.toHaveBeenWarned() + }) + test('comment mismatch (text)', () => { const { container } = mountWithHydration( `
foobar
`, diff --git a/packages/runtime-core/src/hydration.ts b/packages/runtime-core/src/hydration.ts index a94ff35681..aa94eb0961 100644 --- a/packages/runtime-core/src/hydration.ts +++ b/packages/runtime-core/src/hydration.ts @@ -675,7 +675,10 @@ export function createHydrationFunctions( slotScopeIds: string[] | null, isFragment: boolean, ): Node | null => { - if (!isMismatchAllowed(node.parentElement!, MismatchTypes.CHILDREN)) { + if ( + !isMismatchAllowed(node.parentElement!, MismatchTypes.CHILDREN) && + !isCommentNodeMismatch(node, vnode) + ) { ;(__DEV__ || __FEATURE_PROD_HYDRATION_MISMATCH_DETAILS__) && warn( `Hydration node mismatch:\n- rendered on server:`, @@ -993,3 +996,12 @@ function isMismatchAllowed( return allowedAttr.split(',').includes(MismatchTypeString[allowedType]) } } + +// data-allow-mismatch + v-if +function isCommentNodeMismatch(node: Node, vnode: VNode): boolean { + if (node.nodeType !== DOMNodeTypes.COMMENT || !vnode.props) { + return false + } + + return allowMismatchAttr in vnode.props +}