From d563f6686bb100c2545d97d15fe3ca2c026bc842 Mon Sep 17 00:00:00 2001 From: zhoulixiang <18366276315@163.com> Date: Fri, 5 Jan 2024 09:03:57 +0000 Subject: [PATCH] fix: add hydration mismatch checks for attrs --- packages/runtime-core/__tests__/hydration.spec.ts | 3 +++ packages/runtime-core/src/hydration.ts | 6 +++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/runtime-core/__tests__/hydration.spec.ts b/packages/runtime-core/__tests__/hydration.spec.ts index 9e3efdd3f3e..99be4b9c8ab 100644 --- a/packages/runtime-core/__tests__/hydration.spec.ts +++ b/packages/runtime-core/__tests__/hydration.spec.ts @@ -1454,6 +1454,9 @@ describe('SSR hydration', () => { mountWithHydration(``, () => h('textarea', { value: 'foo' }), ) + mountWithHydration(``, () => + h('textarea', { value: '' }), + ) expect(`Hydration attribute mismatch`).not.toHaveBeenWarned() mountWithHydration(`
`, () => h('div', { id: 'foo' })) diff --git a/packages/runtime-core/src/hydration.ts b/packages/runtime-core/src/hydration.ts index 1698204ab62..1436749d1ad 100644 --- a/packages/runtime-core/src/hydration.ts +++ b/packages/runtime-core/src/hydration.ts @@ -739,15 +739,15 @@ function propHasMismatch(el: Element, key: string, clientValue: any): boolean { ) { actual = el.hasAttribute(key) ? el.getAttribute(key) - : el[key as keyof typeof el] + : key in el ? el[key as keyof typeof el] - : false + : '' expected = isBooleanAttr(key) ? includeBooleanAttr(clientValue) ? '' : false : clientValue == null - ? false + ? '' : String(clientValue) if (actual !== expected) { mismatchType = `attribute`