Skip to content

Commit 2206cd2

Browse files
authored
fix(ssr): properly init slots during ssr rendering (#12441)
close #12438
1 parent 5e37dd0 commit 2206cd2

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

packages/runtime-core/src/component.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,7 @@ export function setupComponent(
806806
const { props, children } = instance.vnode
807807
const isStateful = isStatefulComponent(instance)
808808
initProps(instance, props, isStateful, isSSR)
809-
initSlots(instance, children, optimized)
809+
initSlots(instance, children, optimized || isSSR)
810810

811811
const setupResult = isStateful
812812
? setupStatefulComponent(instance, isSSR)

packages/server-renderer/__tests__/ssrSlot.spec.ts

+33-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createApp } from 'vue'
1+
import { createApp, defineAsyncComponent, h } from 'vue'
22
import { renderToString } from '../src/renderToString'
33

44
const components = {
@@ -154,6 +154,38 @@ describe('ssr: slot', () => {
154154
).toBe(`<div><p>1</p><p>2</p></div>`)
155155
})
156156

157+
// #12438
158+
test('async component slot with v-if true', async () => {
159+
const Layout = defineAsyncComponent(() =>
160+
Promise.resolve({
161+
template: `<div><slot name="header">default header</slot></div>`,
162+
}),
163+
)
164+
const LayoutLoader = {
165+
setup(_: any, context: any) {
166+
return () => h(Layout, {}, context.slots)
167+
},
168+
}
169+
expect(
170+
await renderToString(
171+
createApp({
172+
components: {
173+
LayoutLoader,
174+
},
175+
template: `
176+
<Suspense>
177+
<LayoutLoader>
178+
<template v-if="true" #header>
179+
new header
180+
</template>
181+
</LayoutLoader>
182+
</Suspense>
183+
`,
184+
}),
185+
),
186+
).toBe(`<div><!--[--> new header <!--]--></div>`)
187+
})
188+
157189
// #11326
158190
test('dynamic component slot', async () => {
159191
expect(

0 commit comments

Comments
 (0)