8000 fix(hydration): avoid hydration mismatch on style variables with falsy values by yangxiuxiu1115 · Pull Request #12442 · vuejs/core · GitHub
[go: up one dir, main page]

Skip to content

fix(hydration): avoid hydration mismatch on style variables with falsy values #12442

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

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 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
23 changes: 23 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,29 @@ describe('SSR hydration', () => {
app.mount(container)
expect(`Hydration style mismatch`).not.toHaveBeenWarned()
})

test('should not warn on css v-bind non-string & non-number & empty string value', () => {
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('')
useCssVars(() => ({
foo: value1.value,
bar: value2.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 && 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.

I don't think this is necessary, value here is string | undefined.

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
27 changes: 27 additions & 0 deletions packages/runtime-dom/__tests__/helpers/useCssVars.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,4 +465,31 @@ describe('useCssVars', () => {
render(h(App), root)
expect(colorInOnMount).toBe(`red`)
})

test('filter non-string、non-boolean、non-number value', () => {
const state = reactive<any>({
color: 'red',
size: {},
width: false,
height: 10,
})
const root = document.createElement('div')
let style!: CSSStyleDeclaration

const App = {
setup() {
useCssVars(() => state)
onMounted(() => {
style = (root.children[0] as HTMLElement).style
})
return () => h('div')
},
}

render(h(App), root)
expect(style.getPropertyValue(`--color`)).toBe(`red`)
expect(style.getPropertyValue(`--size`)).toBe(``)
expect(style.getPropertyValue(`--width`)).toBe(`false`)
expect(style.getPropertyValue(`--height`)).toBe(`10`)
})
})
10 changes: 7 additions & 3 deletions packages/runtime-dom/src/helpers/useCssVars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
warn,
watch,
} from '@vue/runtime-core'
import { NOOP, ShapeFlags } from '@vue/shared'
import { NOOP, ShapeFlags, isRenderableAttrValue } from '@vue/shared'

export const CSS_VAR_TEXT: unique symbol = Symbol(__DEV__ ? 'CSS_VAR_TEXT' : '')
/**
Expand Down Expand Up @@ -99,8 +99,12 @@ function setVarsOnNode(el: Node, vars: Record<string, string>) {
const style = (el as HTMLElement).style
let cssText = ''
for (const key in vars) {
style.setProperty(`--${key}`, vars[key])
cssText += `--${key}: ${vars[key]};`
const value = vars[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.

A potential breaking change might have been introduced here. Previously, the client would render undefined value, effectively setting the rule value to unset. However, now undefined value will not be rendered, causing the rule to inherit value from the parent node. However, this is an edge case.
Notably, this change aligns with SSR behavior.

Copy link
@adamdehaven adamdehaven Nov 20, 2024

Choose a reason for hiding this comment

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

Note: I don’t think rendering undefined was proper to render regardless; it was essentially setting the value to a string of ’undefined’ which simply doesn’t map to a value.

This worked because undefined is never a valid value in CSS.

In these scenarios where we want to unset and prevent inheritance, it should actually be set to a value of initial instead, which unsets the value in the current scope.

I was thinking you could use @property syntax to declare the custom property and control inheritance; however, this cannot be defined inline on an element.

Copy link
Member

Choose a reason for hiding this comment

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

Unfortunately setting the custom property to initial works the same way as an invalid value as it's treated as a string instead of a global CSS value.

const trimVal = String(value).trim()
style.setProperty(`--${key}`, trimVal)
cssText += `--${key}: ${trimVal};`
}
}
;(style as any)[CSS_VAR_TEXT] = cssText
}
Expand Down
0