Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(hydration): css vars should only be added to expected on component root dom #10325

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 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,21 @@ describe('SSR hydration', () => {
app.mount(container)
expect(`Hydration style mismatch`).not.toHaveBeenWarned()
})

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()
})
})
})
30 changes: 27 additions & 3 deletions packages/runtime-core/src/hydration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -753,9 +753,11 @@ function propHasMismatch(
}
}

const cssVars = instance?.getCssVars?.()
for (const key in cssVars) {
expectedMap.set(`--${key}`, String(cssVars[key]))
if (instance && isInstanceRootEl(instance.subTree, el)) {
const cssVars = instance?.getCssVars?.()
for (const key in cssVars) {
expectedMap.set(`--${key}`, String(cssVars[key]))
}
}

if (!isMapEqual(actualMap, expectedMap)) {
Expand Down Expand Up @@ -852,3 +854,25 @@ function isMapEqual(a: Map<string, string>, b: Map<string, string>): boolean {
}
return true
}

function isInstanceRootEl(vnode: VNode, element: Element): boolean {
while (vnode.component) {
vnode = vnode.component.subTree
}

if (vnode.shapeFlag & ShapeFlags.ELEMENT && vnode.el) {
return element === vnode.el
} else if (vnode.type === Fragment) {
return (vnode.children as VNode[]).some(c => isInstanceRootEl(c, element))
} else if (vnode.type === Static) {
let { el, anchor } = vnode
while (el) {
if (el === element) {
return true
}
if (el === anchor) break
el = el.nextSibling
}
}
return false
}