Skip to content

Commit

Permalink
fix(hydration): fix css vars hydration mismatch false positive on non…
Browse files Browse the repository at this point in the history
…-root nodes

close #10317
test case from #10325
  • Loading branch information
yyx990803 committed Feb 13, 2024
1 parent df4a6e1 commit 995d2fd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
17 changes: 17 additions & 0 deletions packages/runtime-core/__tests__/hydration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1554,5 +1554,22 @@ describe('SSR hydration', () => {
app.mount(container)
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()
})
})
})
12 changes: 9 additions & 3 deletions packages/runtime-core/src/hydration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -753,9 +753,15 @@ function propHasMismatch(
}
}

const cssVars = instance?.getCssVars?.()
for (const key in cssVars) {
expectedMap.set(`--${key}`, String(cssVars[key]))
const root = instance?.subTree
if (
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)) {
Expand Down

0 comments on commit 995d2fd

Please sign in to comment.