Skip to content

Commit 4e467d7

Browse files
authored
fix(ssr): normalize hidden states during hydration (#13125)
1 parent 22b53ea commit 4e467d7

9 files changed

Lines changed: 137 additions & 4 deletions

File tree

‎packages/compiler-dom/__tests__/transforms/stringifyStatic.spec.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,23 @@ describe('stringify static html', () => {
389389
])
390390
})
391391

392+
test.each(['false', '0'])('should remove `hidden` with value `%s`', value => {
393+
const { ast } = compileWithStringify(
394+
`<div>
395+
${repeat(
396+
`<span :hidden="${value}"></span>`,
397+
StringifyThresholds.ELEMENT_WITH_BINDING_COUNT,
398+
)}
399+
</div>`,
400+
)
401+
expect(ast.cached).toMatchObject([
402+
cachedArrayStaticNodeMatcher(
403+
repeat(`<span></span>`, StringifyThresholds.ELEMENT_WITH_BINDING_COUNT),
404+
StringifyThresholds.ELEMENT_WITH_BINDING_COUNT,
405+
),
406+
])
407+
})
408+
392409
test('should stringify svg', () => {
393410
const svg = `<svg width="50" height="50" viewBox="0 0 50 50" fill="none" xmlns="http://www.w3.org/2000/svg">`
394411
const repeated = `<rect width="50" height="50" fill="#C4C4C4"></rect>`

‎packages/compiler-dom/src/transforms/stringifyStatic.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
} from '@vue/compiler-core'
2323
import {
2424
escapeHtml,
25+
includeBooleanAttr,
2526
isArray,
2627
isBooleanAttr,
2728
isKnownHtmlAttr,
@@ -347,7 +348,8 @@ function stringifyElement(
347348
}
348349
// #6568
349350
if (
350-
isBooleanAttr((p.arg as SimpleExpressionNode).content) &&
351+
(isBooleanAttr((p.arg as SimpleExpressionNode).content) ||
352+
(p.arg as SimpleExpressionNode).content === 'hidden') &&
351353
exp.content === 'false'
352354
) {
353355
continue
@@ -356,6 +358,13 @@ function stringifyElement(
356358
let evaluated = evaluateConstant(exp)
357359
if (evaluated != null) {
358360
const arg = p.arg && (p.arg as SimpleExpressionNode).content
361+
if (
362+
arg === 'hidden' &&
363+
typeof evaluated === 'number' &&
364+
!includeBooleanAttr(evaluated)
365+
) {
366+
continue
367+
}
359368
if (arg === 'class') {
360369
evaluated = normalizeClass(evaluated)
361370
} else if (arg === 'style') {

‎packages/compiler-ssr/__tests__/ssrElement.spec.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,20 @@ describe('ssr: element', () => {
191191
`)
192192
})
193193

194+
test('v-bind:arg (hidden)', () => {
195+
expect(
196+
getCompiledString(
197+
`<div><span :hidden="false"></span><span :hidden="'until-found'"></span></div>`,
198+
),
199+
).toMatchInlineSnapshot(`
200+
"\`<div><span\${
201+
_ssrRenderDynamicAttr("hidden", false)
202+
}></span><span\${
203+
_ssrRenderDynamicAttr("hidden", 'until-found')
204+
}></span></div>\`"
205+
`)
206+
})
207+
194208
test('v-bind:[arg]', () => {
195209
expect(getCompiledString(`<div v-bind:[key]="value"></div>`))
196210
.toMatchInlineSnapshot(`

‎packages/compiler-ssr/src/transforms/ssrTransformElement.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,13 @@ export const ssrTransformElement: NodeTransform = (node, context) => {
300300
false /* no newline */,
301301
),
302302
)
303+
} else if (attrName === 'hidden') {
304+
openTag.push(
305+
createCallExpression(
306+
context.helper(SSR_RENDER_DYNAMIC_ATTR),
307+
[key, value],
308+
),
309+
)
303310
} else if (isSSRSafeAttrName(attrName)) {
304311
openTag.push(
305312
createCallExpression(context.helper(SSR_RENDER_ATTR), [

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2487,6 +2487,60 @@ describe('SSR hydration', () => {
24872487
expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
24882488
})
24892489

2490+
test('hidden enumerated attribute', () => {
2491+
mountWithHydration(`<div></div>`, () => h('div', { hidden: false }))
2492+
expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
2493+
2494+
mountWithHydration(`<div hidden></div>`, () => h('div', { hidden: true }))
2495+
expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
2496+
2497+
mountWithHydration(`<div hidden></div>`, () =>
2498+
h('div', { hidden: 'hidden' }),
2499+
)
2500+
expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
2501+
2502+
mountWithHydration(`<div hidden="anything"></div>`, () =>
2503+
h('div', { hidden: true }),
2504+
)
2505+
expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
2506+
2507+
mountWithHydration(`<div hidden="until-found"></div>`, () =>
2508+
h('div', { hidden: 'until-found' }),
2509+
)
2510+
expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
2511+
2512+
mountWithHydration(`<div hidden="UNTIL-FOUND"></div>`, () =>
2513+
h('div', { hidden: 'until-found' }),
2514+
)
2515+
expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
2516+
})
2517+
2518+
test('hidden numeric values', () => {
2519+
mountWithHydration(`<div></div>`, () => h('div', { hidden: 0 }))
2520+
expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
2521+
2522+
mountWithHydration(`<div></div>`, () => h('div', { hidden: NaN }))
2523+
expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
2524+
2525+
mountWithHydration(`<div hidden></div>`, () => h('div', { hidden: 1 }))
2526+
expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
2527+
2528+
mountWithHydration(`<div hidden="0"></div>`, () =>
2529+
h('div', { hidden: '0' }),
2530+
)
2531+
expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
2532+
})
2533+
2534+
test('hidden state mismatch', () => {
2535+
mountWithHydration(`<div hidden="until-found"></div>`, () =>
2536+
h('div', { hidden: true }),
2537+
)
2538+
expect(`Hydration attribute mismatch`).toHaveBeenWarnedTimes(1)
2539+
2540+
mountWithHydration(`<div hidden></div>`, () => h('div', { hidden: 0 }))
2541+
expect(`Hydration attribute mismatch`).toHaveBeenWarnedTimes(2)
2542+
})
2543+
24902544
test('client value is null or undefined', () => {
24912545
mountWithHydration(`<div></div>`, () =>
24922546
h('div', { draggable: undefined }),

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,10 @@ function propHasMismatch(
882882
(el instanceof SVGElement && isKnownSvgAttr(key)) ||
883883
(el instanceof HTMLElement && (isBooleanAttr(key) || isKnownHtmlAttr(key)))
884884
) {
885-
if (isBooleanAttr(key)) {
885+
if (key === 'hidden') {
886+
actual = normalizeHiddenValue(el.getAttribute(key))
887+
expected = normalizeHiddenValue(clientValue)
888+
} else if (isBooleanAttr(key)) {
886889
actual = el.hasAttribute(key)
887890
expected = includeBooleanAttr(clientValue)
888891
} else if (clientValue == null) {
@@ -929,6 +932,18 @@ function propHasMismatch(
929932
return false
930933
}
931934

935+
function normalizeHiddenValue(value: unknown): false | '' | 'until-found' {
936+
if (!isRenderableAttrValue(value)) {
937+
return false
938+
}
939+
if (isString(value)) {
940+
// Attribute values from the DOM are strings, while numeric client values
941+
// follow the `hidden` property setter, where 0 and NaN remove the attribute.
942+
return value.toLowerCase() === 'until-found' ? 'until-found' : ''
943+
}
944+
return includeBooleanAttr(value) ? '' : false
945+
}
946+
932947
function toClassSet(str: string): Set<string> {
933948
return new Set(str.trim().split(/\s+/))
934949
}

‎packages/server-renderer/__tests__/ssrRenderAttrs.spec.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,19 @@ describe('ssr: renderAttrs', () => {
5555
).toBe(` checked disabled`) // boolean attr w/ false should be ignored
5656
})
5757

58+
test('hidden enumerated attribute', () => {
59+
expect(ssrRenderAttrs({ hidden: true })).toBe(` hidden`)
60+
expect(ssrRenderAttrs({ disabled: true, hidden: false })).toBe(` disabled`)
61+
expect(ssrRenderAttrs({ hidden: 'until-found' })).toBe(
62+
` hidden="until-found"`,
63+
)
64+
expect(ssrRenderAttrs({ hidden: '' })).toBe(` hidden`)
65+
expect(ssrRenderAttrs({ hidden: 0 })).toBe(``)
66+
expect(ssrRenderAttrs({ hidden: NaN })).toBe(``)
67+
expect(ssrRenderAttrs({ hidden: 1 })).toBe(` hidden`)
68+
expect(ssrRenderAttrs({ hidden: '0' })).toBe(` hidden="0"`)
69+
})
70+
5871
test('ignore falsy values', () => {
5972
expect(
6073
ssrRenderAttrs({

‎packages/server-renderer/src/helpers/ssrRenderAttrs.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,11 @@ export function ssrRenderDynamicAttr(
7272
tag && (tag.indexOf('-') > 0 || isSVGTag(tag))
7373
? key // preserve raw name on custom elements and svg
7474
: propsToAttrMap[key] || key.toLowerCase()
75-
if (isBooleanAttr(attrKey)) {
75+
if (
76+
isBooleanAttr(attrKey) ||
77+
(attrKey === 'hidden' &&
78+
(typeof value === 'boolean' || typeof value === 'number'))
79+
) {
7680
return includeBooleanAttr(value) ? ` ${attrKey}` : ``
7781
} else if (isSSRSafeAttrName(attrKey)) {
7882
return value === '' ? ` ${attrKey}` : ` ${attrKey}="${escapeHtml(value)}"`

‎packages/shared/src/domAttrConfig.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export const isSpecialBooleanAttr: (key: string) => boolean =
2020
*/
2121
export const isBooleanAttr: (key: string) => boolean = /*@__PURE__*/ makeMap(
2222
specialBooleanAttrs +
23-
`,async,autofocus,autoplay,controls,default,defer,disabled,hidden,` +
23+
`,async,autofocus,autoplay,controls,default,defer,disabled,` +
2424
`inert,loop,open,required,reversed,scoped,seamless,` +
2525
`checked,muted,multiple,selected`,
2626
)

0 commit comments

Comments
 (0)