Skip to content

Commit 836b829

Browse files
authored
fix(compiler-ssr): ensure v-show has a higher priority in SSR (#12171)
close #12162
1 parent 8620a61 commit 836b829

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

packages/compiler-ssr/__tests__/ssrVShow.spec.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ describe('ssr: v-show', () => {
1111
const { ssrRenderAttrs: _ssrRenderAttrs } = require("vue/server-renderer")
1212
1313
return function ssrRender(_ctx, _push, _parent, _attrs) {
14-
_push(\`<div\${_ssrRenderAttrs(_mergeProps({
14+
_push(\`<div\${_ssrRenderAttrs(_mergeProps(_attrs, {
1515
style: (_ctx.foo) ? null : { display: "none" }
16-
}, _attrs))}></div>\`)
16+
}))}></div>\`)
1717
}"
1818
`)
1919
})
@@ -92,6 +92,24 @@ describe('ssr: v-show', () => {
9292
`)
9393
})
9494

95+
test('with style + display', () => {
96+
expect(compileWithWrapper(`<div v-show="foo" style="display:flex" />`).code)
97+
.toMatchInlineSnapshot(`
98+
"const { ssrRenderStyle: _ssrRenderStyle, ssrRenderAttrs: _ssrRenderAttrs } = require("vue/server-renderer")
99+
100+
return function ssrRender(_ctx, _push, _parent, _attrs) {
101+
_push(\`<div\${
102+
_ssrRenderAttrs(_attrs)
103+
}><div style="\${
104+
_ssrRenderStyle([
105+
{"display":"flex"},
106+
(_ctx.foo) ? null : { display: "none" }
107+
])
108+
}"></div></div>\`)
109+
}"
110+
`)
111+
})
112+
95113
test('with v-bind', () => {
96114
expect(
97115
compileWithWrapper(

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,17 @@ export const ssrTransformElement: NodeTransform = (node, context) => {
8888
const hasCustomDir = node.props.some(
8989
p => p.type === NodeTypes.DIRECTIVE && !isBuiltInDirective(p.name),
9090
)
91+
92+
// v-show has a higher priority in ssr
93+
const vShowPropIndex = node.props.findIndex(
94+
i => i.type === NodeTypes.DIRECTIVE && i.name === 'show',
95+
)
96+
if (vShowPropIndex !== -1) {
97+
const vShowProp = node.props[vShowPropIndex]
98+
node.props.splice(vShowPropIndex, 1)
99+
node.props.push(vShowProp)
100+
}
101+
91102
const needMergeProps = hasDynamicVBind || hasCustomDir
92103
if (needMergeProps) {
93104
const { props, directives } = buildProps(

0 commit comments

Comments
 (0)