Skip to content

Commit 972face

Browse files
committed
fix(hydration): improve attr hydration mismatch check for boolean attrs
close #10057 close #10060
1 parent d60a575 commit 972face

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1489,5 +1489,20 @@ describe('SSR hydration', () => {
14891489
mountWithHydration(`<div id="bar"></div>`, () => h('div', { id: 'foo' }))
14901490
expect(`Hydration attribute mismatch`).toHaveBeenWarnedTimes(2)
14911491
})
1492+
1493+
test('boolean attr handling', () => {
1494+
mountWithHydration(`<input />`, () => h('input', { readonly: false }))
1495+
expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
1496+
1497+
mountWithHydration(`<input readonly />`, () =>
1498+
h('input', { readonly: true }),
1499+
)
1500+
expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
1501+
1502+
mountWithHydration(`<input readonly="readonly" />`, () =>
1503+
h('input', { readonly: true }),
1504+
)
1505+
expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
1506+
})
14921507
})
14931508
})

packages/runtime-core/src/hydration.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -754,19 +754,18 @@ function propHasMismatch(
754754
(el instanceof SVGElement && isKnownSvgAttr(key)) ||
755755
(el instanceof HTMLElement && (isBooleanAttr(key) || isKnownHtmlAttr(key)))
756756
) {
757-
// #10000 some attrs such as textarea.value can't be get by `hasAttribute`
758-
actual = el.hasAttribute(key)
759-
? el.getAttribute(key)
760-
: key in el
761-
? el[key as keyof typeof el]
762-
: ''
763-
expected = isBooleanAttr(key)
764-
? includeBooleanAttr(clientValue)
765-
? ''
766-
: false
767-
: clientValue == null
768-
? ''
769-
: String(clientValue)
757+
if (isBooleanAttr(key)) {
758+
actual = el.hasAttribute(key)
759+
expected = includeBooleanAttr(clientValue)
760+
} else {
761+
// #10000 some attrs such as textarea.value can't be get by `hasAttribute`
762+
actual = el.hasAttribute(key)
763+
? el.getAttribute(key)
764+
: key in el
765+
? el[key as keyof typeof el]
766+
: ''
767+
expected = clientValue == null ? '' : String(clientValue)
768+
}
770769
if (actual !== expected) {
771770
mismatchType = `attribute`
772771
mismatchKey = key

0 commit comments

Comments
 (0)