Skip to content

Commit 70c2d5b

Browse files
committed
fix(ssr): fix ssr render output for fragment in slots
fix #5859
1 parent cbeb9f2 commit 70c2d5b

File tree

2 files changed

+88
-2
lines changed

2 files changed

+88
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* @jest-environment node
3+
*/
4+
5+
import { createApp } from 'vue'
6+
import { renderToString } from '../src/renderToString'
7+
8+
const components = {
9+
one: {
10+
template: `<div><slot/></div>`
11+
}
12+
}
13+
14+
describe('ssr: slot', () => {
15+
test('text slot', async () => {
16+
expect(
17+
await renderToString(
18+
createApp({
19+
components,
20+
template: `<one>hello</one>`
21+
})
22+
)
23+
).toBe(`<div><!--[-->hello<!--]--></div>`)
24+
})
25+
26+
test('element slot', async () => {
27+
expect(
28+
await renderToString(
29+
createApp({
30+
components,
31+
template: `<one><div>hi</div></one>`
32+
})
33+
)
34+
).toBe(`<div><!--[--><div>hi</div><!--]--></div>`)
35+
})
36+
37+
test('empty slot', async () => {
38+
expect(
39+
await renderToString(
40+
createApp({
41+
components: {
42+
one: {
43+
template: `<div><slot/></div>`
44+
}
45+
},
46+
template: `<one><template v-if="false"/></one>`
47+
})
48+
)
49+
).toBe(`<div><!--[--><!--]--></div>`)
50+
})
51+
52+
test('multiple elements', async () => {
53+
expect(
54+
await renderToString(
55+
createApp({
56+
components,
57+
template: `<one><div>one</div><div>two</div></one>`
58+
})
59+
)
60+
).toBe(`<div><!--[--><div>one</div><div>two</div><!--]--></div>`)
61+
})
62+
63+
test('fragment slot (template v-if)', async () => {
64+
expect(
65+
await renderToString(
66+
createApp({
67+
components,
68+
template: `<one><template v-if="true">hello</template></one>`
69+
})
70+
)
71+
).toBe(`<div><!--[--><!--[-->hello<!--]--><!--]--></div>`)
72+
})
73+
74+
test('fragment slot (template v-if + multiple elements)', async () => {
75+
expect(
76+
await renderToString(
77+
createApp({
78+
components,
79+
template: `<one><template v-if="true"><div>one</div><div>two</div></template></one>`
80+
})
81+
)
82+
).toBe(
83+
`<div><!--[--><!--[--><div>one</div><div>two</div><!--]--><!--]--></div>`
84+
)
85+
})
86+
})

packages/server-renderer/src/helpers/ssrRenderSlot.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export function ssrRenderSlotInner(
8282
}
8383
}
8484

85-
const commentRE = /^<!--.*-->$/
85+
const commentRE = /<!--.*?-->/g
8686
function isComment(item: SSRBufferItem) {
87-
return typeof item === 'string' && commentRE.test(item)
87+
return typeof item === 'string' && !item.replace(commentRE, '').trim()
8888
}

0 commit comments

Comments
 (0)