Skip to content

Commit 71bc601

Browse files
committed
fix(server-renderer): render cloneVNode attrs on the root of vapor slot content
1 parent e9310d6 commit 71bc601

15 files changed

Lines changed: 1394 additions & 53 deletions

File tree

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,18 @@ describe('ssr: components', () => {
7474
compile(`<component :is="foo"><div/></component>`, { vapor: true }).code,
7575
).toMatchInlineSnapshot(`
7676
"const { resolveDynamicComponent: _resolveDynamicComponent, withCtx: _withCtx, createVNode: _createVNode } = require("vue")
77-
const { ssrRenderDynamicComponent: _ssrRenderDynamicComponent } = require("vue/server-renderer")
77+
const { ssrRenderAttrs: _ssrRenderAttrs, ssrRenderDynamicComponent: _ssrRenderDynamicComponent } = require("vue/server-renderer")
7878
7979
return function ssrRender(_ctx, _push, _parent, _attrs) {
8080
_push(\`<!--{-->\`)
8181
_ssrRenderDynamicComponent(_push, _resolveDynamicComponent(_ctx.foo), _attrs, {
82-
default: _withCtx((_, _push, _parent, _scopeId) => {
82+
default: _withCtx((_, _push, _parent, _scopeId, _slotAttrs) => {
8383
if (_push) {
84-
_push(\`<div\${_scopeId}></div>\`)
84+
_push(\`<div\${
85+
_slotAttrs ? _ssrRenderAttrs(_slotAttrs) : \`\`
86+
}\${
87+
_scopeId
88+
}></div>\`)
8589
} else {
8690
return [
8791
_createVNode("div")

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ describe('ssr: element', () => {
6565
_push(\`<textarea\${
6666
_ssrRenderAttrs(_temp0 = _mergeProps(_ctx.obj, _attrs), "textarea")
6767
}>\${
68-
_ssrInterpolate(("value" in _temp0) ? _temp0.value : "fallback")
68+
_ssrInterpolate((_temp0 && "value" in _temp0) ? _temp0.value : "fallback")
6969
}</textarea>\`)
7070
}"
7171
`)
@@ -87,7 +87,7 @@ describe('ssr: element', () => {
8787
}<textarea\${
8888
_ssrRenderAttrs(_temp0 = _ctx.a, "textarea")
8989
}>\${
90-
_ssrInterpolate(("value" in _temp0) ? _temp0.value : "")
90+
_ssrInterpolate((_temp0 && "value" in _temp0) ? _temp0.value : "")
9191
}</textarea></div>\`)
9292
}"
9393
`)

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

Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,239 @@ describe('ssr: attrs fallthrough', () => {
9393
}"
9494
`)
9595
})
96+
97+
// what a vdom `cloneVNode` adds to the content of a vapor slot comes as the
98+
// slot function's `_slotAttrs`, for the single root of the slot body
99+
describe('vapor slot root', () => {
100+
const ssr = (src: string, vapor = true) =>
101+
compile(src, { vapor }).code.replace(/\s*\n\s*/g, '')
102+
103+
// merged only when there are attrs: the tag is otherwise as it would be
104+
test('element', () => {
105+
expect(
106+
compile(`<Comp><button class="a">open</button></Comp>`, {
107+
vapor: true,
108+
}).code,
109+
).toMatchInlineSnapshot(`
110+
"const { resolveComponent: _resolveComponent, mergeProps: _mergeProps, withCtx: _withCtx, createVNode: _createVNode } = require("vue")
111+
const { ssrRenderAttrs: _ssrRenderAttrs, ssrRenderComponent: _ssrRenderComponent } = require("vue/server-renderer")
112+
113+
return function ssrRender(_ctx, _push, _parent, _attrs) {
114+
const _component_Comp = _resolveComponent("Comp")
115+
116+
_push(\`<!--{-->\`)
117+
_push(_ssrRenderComponent(_component_Comp, _attrs, {
118+
default: _withCtx((_, _push, _parent, _scopeId, _slotAttrs) => {
119+
if (_push) {
120+
_push(\`<button\${
121+
_slotAttrs ? _ssrRenderAttrs(_mergeProps({ class: "a" }, _slotAttrs)) : \` class="a"\`
122+
}\${
123+
_scopeId
124+
}>open</button>\`)
125+
} else {
126+
return [
127+
_createVNode("button", { class: "a" }, "open")
128+
]
129+
}
130+
}),
131+
_: 1 /* STABLE */
132+
}, _parent))
133+
_push(\`<!--}-->\`)
134+
}"
135+
`)
136+
})
137+
138+
const bare = (tag: string) =>
139+
`<${tag}\${_slotAttrs ? _ssrRenderAttrs(_slotAttrs) : \`\`}\${_scopeId}>`
140+
141+
test.each([
142+
[
143+
'element with bindings',
144+
`<Comp><i :id="id" class="a" :class="b" v-show="ok" /></Comp>`,
145+
[
146+
'_slotAttrs ? ' +
147+
'_ssrRenderAttrs(_mergeProps({id: _ctx.id,class: ["a", _ctx.b]}, _slotAttrs, {style: (_ctx.ok) ? null : { display: "none" }}))' +
148+
' : `${_ssrRenderAttr("id", _ctx.id)} class="${_ssrRenderClass([_ctx.b, "a"])}" style="${_ssrRenderStyle((_ctx.ok) ? null : { display: "none" })}"`',
149+
],
150+
],
151+
[
152+
'v-model input',
153+
`<Comp><input v-model="s" class="a"></Comp>`,
154+
[
155+
'_slotAttrs ? ' +
156+
'_ssrRenderAttrs((_temp0 = _mergeProps({ class: "a" }, _slotAttrs), _mergeProps(_temp0, _ssrGetDynamicModelProps(_temp0, _ctx.s))))' +
157+
' : `${_ssrRenderAttr("value", _ctx.s)} class="a"`',
158+
],
159+
],
160+
[
161+
'component',
162+
`<Comp><Btn foo="1" /></Comp>`,
163+
[
164+
'_push(_ssrRenderComponent(_component_Btn, _slotAttrs ? _mergeProps({ foo: "1" }, _slotAttrs) : { foo: "1" }, null, _parent, _scopeId))',
165+
],
166+
],
167+
[
168+
'component with v-bind',
169+
`<Comp><Btn v-bind="obj" foo="1" /></Comp>`,
170+
[
171+
'_push(_ssrRenderComponent(_component_Btn, _mergeProps(_ctx.obj, { foo: "1" }, _slotAttrs), null, _parent, _scopeId))',
172+
],
173+
],
174+
[
175+
'dynamic component',
176+
`<Comp><component :is="x" /></Comp>`,
177+
[
178+
'_ssrRenderDynamicComponent(_push, _resolveDynamicComponent(_ctx.x), _slotAttrs, null, _parent, _scopeId)',
179+
],
180+
],
181+
[
182+
'v-if chain',
183+
`<Comp><i v-if="ok" /><b v-else /></Comp>`,
184+
[bare('i'), bare('b')],
185+
],
186+
[
187+
'template v-if',
188+
`<Comp><template v-if="ok"><i /></template></Comp>`,
189+
[bare('i')],
190+
],
191+
[
192+
'v-if chain in a template v-if',
193+
`<Comp><template v-if="a"><i v-if="b" /><b v-else /></template></Comp>`,
194+
[bare('i'), bare('b')],
195+
],
196+
[
197+
'template v-if with a comment',
198+
`<Comp><template v-if="a"><!--c--><i /></template></Comp>`,
199+
[bare('i')],
200+
],
201+
[
202+
'template v-if in a template v-if',
203+
`<Comp><template v-if="a"><template v-if="b"><i /></template></template></Comp>`,
204+
[bare('i')],
205+
],
206+
[
207+
'v-if chain in a slot template',
208+
`<Comp><template #a><i v-if="ok" /><b v-else /></template></Comp>`,
209+
[bare('i'), bare('b')],
210+
],
211+
[
212+
'slot template with a comment',
213+
`<Comp><template #a><!--c--><i /></template></Comp>`,
214+
[bare('i')],
215+
],
216+
[
217+
'slot template, next to the implicit default slot',
218+
`<Comp><template #a><i /></template><b /></Comp>`,
219+
[bare('i'), bare('b')],
220+
],
221+
[
222+
'the slot of a nested component',
223+
`<Comp><div><Inner><p /></Inner></div></Comp>`,
224+
[bare('div'), bare('p')],
225+
],
226+
[
227+
'Transition child',
228+
`<Comp><Transition><i v-if="ok" /></Transition></Comp>`,
229+
[bare('i')],
230+
],
231+
[
232+
'Transition branches',
233+
`<Comp><Transition><i v-if="ok" /><b v-else /></Transition></Comp>`,
234+
[bare('i'), bare('b')],
235+
],
236+
[
237+
'KeepAlive child',
238+
`<Comp><KeepAlive><Btn /></KeepAlive></Comp>`,
239+
[
240+
'_push(_ssrRenderComponent(_component_Btn, _slotAttrs, null, _parent, _scopeId))',
241+
],
242+
],
243+
// these merge props, or render content off them, either way
244+
[
245+
'element with v-bind',
246+
`<Comp><i v-bind="obj" class="a" /></Comp>`,
247+
[
248+
'<i${_ssrRenderAttrs(_mergeProps(_ctx.obj, { class: "a" }, _slotAttrs))}${_scopeId}>',
249+
],
250+
],
251+
[
252+
'v-model select',
253+
`<Comp><select v-model="s" class="a" /></Comp>`,
254+
[
255+
'<select${_ssrRenderAttrs(_mergeProps({ class: "a" }, _slotAttrs))}${_scopeId}>',
256+
],
257+
],
258+
[
259+
'textarea',
260+
`<Comp><textarea v-model="s" class="a" /></Comp>`,
261+
[
262+
'<textarea${_ssrRenderAttrs(_mergeProps({ class: "a" }, _slotAttrs), "textarea")}${_scopeId}>',
263+
],
264+
],
265+
// no attrs when the content is not cloned
266+
[
267+
'textarea without props',
268+
`<Comp><textarea>hello</textarea></Comp>`,
269+
[
270+
'<textarea${_ssrRenderAttrs(_temp0 = _slotAttrs, "textarea")}${_scopeId}>${_ssrInterpolate((_temp0 && "value" in _temp0) ? _temp0.value : "hello")}</textarea>',
271+
],
272+
],
273+
])('%s', (_, src, expected) => {
274+
const code = ssr(src)
275+
for (const part of expected) expect(code).toContain(part)
276+
})
277+
278+
test.each([
279+
['several roots', `<Comp><i /><b /></Comp>`],
280+
['v-for', `<Comp><i v-for="n in 2" /></Comp>`],
281+
['text', `<Comp>{{ msg }}</Comp>`],
282+
['slot outlet', `<Comp><slot /></Comp>`],
283+
['below the root', `<Comp><div><i /></div><b /></Comp>`],
284+
[
285+
'several roots in a template v-if',
286+
`<Comp><template v-if="a"><i /><b /></template></Comp>`,
287+
],
288+
[
289+
'a v-if next to another root in a template v-if',
290+
`<Comp><template v-if="a"><i v-if="b" /><b /></template></Comp>`,
291+
],
292+
[
293+
'Transition below an element',
294+
`<div><Transition><i /></Transition></div>`,
295+
],
296+
['Suspense slot', `<Suspense><i /></Suspense>`],
297+
[
298+
'Suspense slot template',
299+
`<Suspense><template #fallback><i v-if="a" /><b v-else /></template></Suspense>`,
300+
],
301+
[
302+
'several roots in a slot template',
303+
`<Comp><template #a><i v-if="a" /><b /></template></Comp>`,
304+
],
305+
['Teleport content', `<Teleport to="body"><i /></Teleport>`],
306+
[
307+
'TransitionGroup child',
308+
`<TransitionGroup><i key="a" /></TransitionGroup>`,
309+
],
310+
])('not for %s', (_, src) => {
311+
// but as the parameter
312+
expect(ssr(src)).not.toMatch(/_slotAttrs(?!\) =>)/)
313+
})
314+
315+
// a component's own root keeps its rule
316+
test.each([
317+
`<template v-if="a"><i v-if="b" /><b v-else /></template>`,
318+
`<template v-if="a"><!--c--><i /></template>`,
319+
])('not for the root in %s', src => {
320+
expect(ssr(src)).not.toMatch(/_attrs\b(?!\) {)/)
321+
})
322+
323+
test('not in a vdom component', () => {
324+
const code = ssr(`<Comp><button class="a">open</button></Comp>`, false)
325+
expect(code).not.toContain('_slotAttrs')
326+
expect(code).toContain(
327+
'_push(`<button class="a"${_scopeId}>open</button>`)',
328+
)
329+
})
330+
})
96331
})

0 commit comments

Comments
 (0)