fix(hydration): fix css vars hydration mismatch false positive on non-root nodes

close #10317
test case from #10325
This commit is contained in:
Evan You 2024-02-13 18:00:09 +08:00
parent df4a6e1dd8
commit 995d2fdcca
2 changed files with 26 additions and 3 deletions

View File

@ -1554,5 +1554,22 @@ describe('SSR hydration', () => {
app.mount(container) app.mount(container)
expect(`Hydration style mismatch`).not.toHaveBeenWarned() expect(`Hydration style mismatch`).not.toHaveBeenWarned()
}) })
// #10317 - test case from #10325
test('css vars should only be added to expected on component root dom', () => {
const container = document.createElement('div')
container.innerHTML = `<div style="--foo:red;"><div style="color:var(--foo);" /></div>`
const app = createSSRApp({
setup() {
useCssVars(() => ({
foo: 'red',
}))
return () =>
h('div', null, [h('div', { style: { color: 'var(--foo)' } })])
},
})
app.mount(container)
expect(`Hydration style mismatch`).not.toHaveBeenWarned()
})
}) })
}) })

View File

@ -753,9 +753,15 @@ function propHasMismatch(
} }
} }
const cssVars = instance?.getCssVars?.() const root = instance?.subTree
for (const key in cssVars) { if (
expectedMap.set(`--${key}`, String(cssVars[key])) vnode === root ||
(root?.type === Fragment && (root.children as VNode[]).includes(vnode))
) {
const cssVars = instance?.getCssVars?.()
for (const key in cssVars) {
expectedMap.set(`--${key}`, String(cssVars[key]))
}
} }
if (!isMapEqual(actualMap, expectedMap)) { if (!isMapEqual(actualMap, expectedMap)) {