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): avoid hydration mismatch on style variables with falsy values #12442

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions packages/runtime-core/__tests__/hydration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2204,6 +2204,31 @@ describe('SSR hydration', () => {
app.mount(container)
expect(`Hydration style mismatch`).not.toHaveBeenWarned()
})

test('style var with falsy values', () => {
const container = document.createElement('div')
container.innerHTML = `<div style="padding: 4px;--bar:;"></div>`
const app = createSSRApp({
setup() {
const value1 = ref<any>(null)
const value2 = ref('')
const value3 = ref<any>(undefined)
useCssVars(() => ({
foo: value1.value,
bar: value2.value,
baz: value3.value,
}))
return () => h(Child)
},
})
const Child = {
setup() {
return () => h('div', { style: 'padding: 4px' })
},
}
app.mount(container)
expect(`Hydration style mismatch`).not.toHaveBeenWarned()
})
})

describe('data-allow-mismatch', () => {
Expand Down
13 changes: 8 additions & 5 deletions packages/runtime-core/src/hydration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,7 @@ function toStyleMap(str: string): Map<string, string> {
let [key, value] = item.split(':')
key = key.trim()
value = value && value.trim()
if (key && value) {
if (key) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the value has already been validated using isRenderableAttrValue during SSR,

styleMap.set(key, value)
}
}
Expand Down Expand Up @@ -938,10 +938,13 @@ function resolveCssVars(
) {
const cssVars = instance.getCssVars()
for (const key in cssVars) {
expectedMap.set(
`--${getEscapedCssVarName(key, false)}`,
String(cssVars[key]),
)
const value = cssVars[key]
if (isRenderableAttrValue(value)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And I think it's not semantically correct to use isRenderableAttrValue here as the value isn't to be rendered as a DOM attribute. /cc @edison1105

expectedMap.set(
`--${getEscapedCssVarName(key, false)}`,
String(value).trim(),
)
}
}
}
if (vnode === root && instance.parent) {
Expand Down