Skip to content

Commit 995d2fd

Browse files
committedFeb 13, 2024·
fix(hydration): fix css vars hydration mismatch false positive on non-root nodes
close #10317 test case from #10325
1 parent df4a6e1 commit 995d2fd

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed
 

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

+17
Original file line numberDiff line numberDiff line change
@@ -1554,5 +1554,22 @@ describe('SSR hydration', () => {
15541554
app.mount(container)
15551555
expect(`Hydration style mismatch`).not.toHaveBeenWarned()
15561556
})
1557+
1558+
// #10317 - test case from #10325
1559+
test('css vars should only be added to expected on component root dom', () => {
1560+
const container = document.createElement('div')
1561+
container.innerHTML = `<div style="--foo:red;"><div style="color:var(--foo);" /></div>`
1562+
const app = createSSRApp({
1563+
setup() {
1564+
useCssVars(() => ({
1565+
foo: 'red',
1566+
}))
1567+
return () =>
1568+
h('div', null, [h('div', { style: { color: 'var(--foo)' } })])
1569+
},
1570+
})
1571+
app.mount(container)
1572+
expect(`Hydration style mismatch`).not.toHaveBeenWarned()
1573+
})
15571574
})
15581575
})

‎packages/runtime-core/src/hydration.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -753,9 +753,15 @@ function propHasMismatch(
753753
}
754754
}
755755

756-
const cssVars = instance?.getCssVars?.()
757-
for (const key in cssVars) {
758-
expectedMap.set(`--${key}`, String(cssVars[key]))
756+
const root = instance?.subTree
757+
if (
758+
vnode === root ||
759+
(root?.type === Fragment && (root.children as VNode[]).includes(vnode))
760+
) {
761+
const cssVars = instance?.getCssVars?.()
762+
for (const key in cssVars) {
763+
expectedMap.set(`--${key}`, String(cssVars[key]))
764+
}
759765
}
760766

761767
if (!isMapEqual(actualMap, expectedMap)) {

0 commit comments

Comments
 (0)
Please sign in to comment.