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(ssr): avoid merging style generated by vShow into $attrs #12516

Open
wants to merge 4 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
38 changes: 38 additions & 0 deletions packages/compiler-ssr/__tests__/ssrVShow.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,44 @@ describe('ssr: v-show', () => {
`)
})

test('with component', () => {
expect(
compileWithWrapper(`<Foo :style="{color:'red'}" v-show="foo"/>`).code,
).toMatchInlineSnapshot(`
"const { resolveComponent: _resolveComponent } = require("vue")
const { ssrRenderComponent: _ssrRenderComponent, ssrRenderAttrs: _ssrRenderAttrs } = require("vue/server-renderer")

return function ssrRender(_ctx, _push, _parent, _attrs) {
const _component_Foo = _resolveComponent("Foo")

_push(\`<div\${_ssrRenderAttrs(_attrs)}>\`)
_push(_ssrRenderComponent(_component_Foo, { style: {color:'red'} }, null, _parent, undefined, {
style: (_ctx.foo) ? null : { display: "none" }
}))
_push(\`</div>\`)
}"
`)
})

test('with dynamic component', () => {
expect(
compileWithWrapper(
`<component is="Foo" :style="{color:'red'}" v-show="foo"/>`,
).code,
).toMatchInlineSnapshot(`
"const { resolveDynamicComponent: _resolveDynamicComponent, createVNode: _createVNode } = require("vue")
const { ssrRenderVNode: _ssrRenderVNode, ssrRenderAttrs: _ssrRenderAttrs } = require("vue/server-renderer")

return function ssrRender(_ctx, _push, _parent, _attrs) {
_push(\`<div\${_ssrRenderAttrs(_attrs)}>\`)
_ssrRenderVNode(_push, _createVNode(_resolveDynamicComponent("Foo"), { style: {color:'red'} }, null), _parent, undefined, {
style: (_ctx.foo) ? null : { display: "none" }
})
_push(\`</div>\`)
}"
`)
})

test('with static style', () => {
expect(compileWithWrapper(`<div style="color:red" v-show="foo"/>`).code)
.toMatchInlineSnapshot(`
Expand Down
41 changes: 31 additions & 10 deletions packages/compiler-ssr/src/transforms/ssrTransformComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
createCallExpression,
createFunctionExpression,
createIfStatement,
createObjectExpression,
createReturnStatement,
createRoot,
createSimpleExpression,
Expand Down Expand Up @@ -134,6 +135,17 @@ export const ssrTransformComponent: NodeTransform = (node, context) => {
})
}

let vShowExp
const vShowDir = node.props.find(
p => p.type === NodeTypes.DIRECTIVE && p.name === 'show',
) as DirectiveNode | undefined
if (vShowDir) {
node.props = node.props.filter(p => p !== vShowDir)
const directiveTransform = context.directiveTransforms['show']
const { props } = directiveTransform!(vShowDir, node, context)
vShowExp = createObjectExpression(props)
}

let propsExp: string | JSChildNode = `null`
if (node.props.length) {
// note we are not passing ssr: true here because for components, v-on
Expand Down Expand Up @@ -180,22 +192,31 @@ export const ssrTransformComponent: NodeTransform = (node, context) => {
// dynamic component that resolved to a `resolveDynamicComponent` call
// expression - since the resolved result may be a plain element (string)
// or a VNode, handle it with `renderVNode`.
const args: (string | JSChildNode)[] = [
`_push`,
createCallExpression(context.helper(CREATE_VNODE), [
component,
propsExp,
slots,
]),
`_parent`,
]
if (vShowExp) args.push(`undefined`, vShowExp)
node.ssrCodegenNode = createCallExpression(
context.helper(SSR_RENDER_VNODE),
[
`_push`,
createCallExpression(context.helper(CREATE_VNODE), [
component,
propsExp,
slots,
]),
`_parent`,
],
args,
)
} else {
const args: (string | JSChildNode)[] = [
component,
propsExp,
slots,
`_parent`,
]
if (vShowExp) args.push(`undefined`, vShowExp)
node.ssrCodegenNode = createCallExpression(
context.helper(SSR_RENDER_COMPONENT),
[component, propsExp, slots, `_parent`],
args,
)
}
}
Expand Down
32 changes: 32 additions & 0 deletions packages/server-renderer/__tests__/ssrDirectives.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,38 @@ describe('ssr: directives', () => {
),
).toBe(`<div style="color:red;font-size:12;display:none;"></div>`)
})

test('with component', async () => {
expect(
await renderToString(
createApp({
components: {
Foo: {
template: `<div><span v-bind="$attrs"></span></div>`,
},
},
data: () => ({ show: false }),
template: `<Foo v-show="show"/>`,
}),
),
).toBe(`<div style="display:none;"><span></span></div>`)
})

test('with dynamic component', async () => {
expect(
await renderToString(
createApp({
components: {
Foo: {
template: `<div><span v-bind="$attrs"></span></div>`,
},
},
data: () => ({ show: false }),
template: `<component is="Foo" v-show="show"/>`,
}),
),
).toBe(`<div style="display:none;"><span></span></div>`)
})
})

describe('template v-model', () => {
Expand Down
2 changes: 2 additions & 0 deletions packages/server-renderer/src/helpers/ssrRenderComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ export function ssrRenderComponent(
children: Slots | SSRSlots | null = null,
parentComponent: ComponentInternalInstance | null = null,
slotScopeId?: string,
vShowValue?: Props,
): SSRBuffer | Promise<SSRBuffer> {
return renderComponentVNode(
createVNode(comp, props, children),
parentComponent,
slotScopeId,
vShowValue,
)
}
19 changes: 16 additions & 3 deletions packages/server-renderer/src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export function renderComponentVNode(
vnode: VNode,
parentComponent: ComponentInternalInstance | null = null,
slotScopeId?: string,
vShowValue?: Props,
): SSRBuffer | Promise<SSRBuffer> {
const instance = (vnode.component = createComponentInstance(
vnode,
Expand All @@ -116,15 +117,18 @@ export function renderComponentVNode(
})
// Note: error display is already done by the wrapped lifecycle hook function.
.catch(NOOP)
return p.then(() => renderComponentSubTree(instance, slotScopeId))
return p.then(() =>
renderComponentSubTree(instance, slotScopeId, vShowValue),
)
} else {
return renderComponentSubTree(instance, slotScopeId)
return renderComponentSubTree(instance, slotScopeId, vShowValue)
}
}

function renderComponentSubTree(
instance: ComponentInternalInstance,
slotScopeId?: string,
vShowValue?: Props,
): SSRBuffer | Promise<SSRBuffer> {
if (__DEV__) pushWarningContext(instance.vnode)
const comp = instance.type as Component
Expand Down Expand Up @@ -186,6 +190,12 @@ function renderComponentSubTree(
}
}

if (vShowValue) {
// v-show has higher priority
if (attrs) attrs = mergeProps(attrs, vShowValue)
else attrs = vShowValue
}

// set current rendering instance for asset resolution
const prev = setCurrentRenderingInstance(instance)
try {
Expand Down Expand Up @@ -225,6 +235,7 @@ export function renderVNode(
vnode: VNode,
parentComponent: ComponentInternalInstance,
slotScopeId?: string,
vShowValue?: Props,
): void {
const { type, shapeFlag, children, dirs, props } = vnode
if (dirs) {
Expand Down Expand Up @@ -263,7 +274,9 @@ export function renderVNode(
if (shapeFlag & ShapeFlags.ELEMENT) {
renderElementVNode(push, vnode, parentComponent, slotScopeId)
} else if (shapeFlag & ShapeFlags.COMPONENT) {
push(renderComponentVNode(vnode, parentComponent, slotScopeId))
push(
renderComponentVNode(vnode, parentComponent, slotScopeId, vShowValue),
)
} else if (shapeFlag & ShapeFlags.TELEPORT) {
renderTeleportVNode(push, vnode, parentComponent, slotScopeId)
} else if (shapeFlag & ShapeFlags.SUSPENSE) {
Expand Down
Loading