Skip to content

Commit a46f3b3

Browse files
committed
fix(compiler-ssr): should pass necessary tag names for dynamic v-bind
1 parent 3b40fc5 commit a46f3b3

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,27 @@ describe('ssr: element', () => {
5757
let _temp0
5858
5959
_push(\`<textarea\${
60-
_ssrRenderAttrs(_temp0 = _ctx.obj)
60+
_ssrRenderAttrs(_temp0 = _ctx.obj, \\"textarea\\")
6161
}>\${
6262
_ssrInterpolate((\\"value\\" in _temp0) ? _temp0.value : \\"fallback\\")
6363
}</textarea>\`)
6464
}"
6565
`)
6666
})
67+
68+
test('should pass tag to custom elements w/ dynamic v-bind', () => {
69+
expect(
70+
compile(`<my-foo v-bind="obj"></my-foo>`, {
71+
isCustomElement: () => true
72+
}).code
73+
).toMatchInlineSnapshot(`
74+
"const { ssrRenderAttrs: _ssrRenderAttrs } = require(\\"@vue/server-renderer\\")
75+
76+
return function ssrRender(_ctx, _push, _parent) {
77+
_push(\`<my-foo\${_ssrRenderAttrs(_ctx.obj, \\"my-foo\\")}></my-foo>\`)
78+
}"
79+
`)
80+
})
6781
})
6882

6983
describe('attrs', () => {

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ export const ssrTransformElement: NodeTransform = (node, context) => {
6060
// element
6161
// generate the template literal representing the open tag.
6262
const openTag: TemplateLiteral['elements'] = [`<${node.tag}`]
63+
// some tags need to be pasesd to runtime for special checks
64+
const needTagForRuntime =
65+
node.tag === 'textarea' || node.tag.indexOf('-') > 0
6366

6467
// v-bind="obj" or v-bind:[key] can potentially overwrite other static
6568
// attrs and can affect final rendering result, so when they are present
@@ -79,10 +82,12 @@ export const ssrTransformElement: NodeTransform = (node, context) => {
7982
// assign the merged props to a temp variable, and check whether
8083
// it contains value (if yes, render is as children).
8184
const tempId = `_temp${context.temps++}`
82-
propsExp.arguments[0] = createAssignmentExpression(
83-
createSimpleExpression(tempId, false),
84-
props
85-
)
85+
propsExp.arguments = [
86+
createAssignmentExpression(
87+
createSimpleExpression(tempId, false),
88+
props
89+
)
90+
]
8691
const existingText = node.children[0] as TextNode | undefined
8792
rawChildrenMap.set(
8893
node,
@@ -125,6 +130,10 @@ export const ssrTransformElement: NodeTransform = (node, context) => {
125130
}
126131
}
127132

133+
if (needTagForRuntime) {
134+
propsExp.arguments.push(`"${node.tag}"`)
135+
}
136+
128137
openTag.push(propsExp)
129138
}
130139
}
@@ -234,10 +243,14 @@ export const ssrTransformElement: NodeTransform = (node, context) => {
234243
// dynamic key attr
235244
// this branch is only encountered for custom directive
236245
// transforms that returns properties with dynamic keys
246+
const args: CallExpression['arguments'] = [key, value]
247+
if (needTagForRuntime) {
248+
args.push(`"${node.tag}"`)
249+
}
237250
openTag.push(
238251
createCallExpression(
239252
context.helper(SSR_RENDER_DYNAMIC_ATTR),
240-
[key, value]
253+
args
241254
)
242255
)
243256
}

0 commit comments

Comments
 (0)