From 12fef2d57aecf5cb91d3e642301a0ae5be73fcce Mon Sep 17 00:00:00 2001 From: daiwei Date: Wed, 20 Nov 2024 09:56:53 +0800 Subject: [PATCH 1/3] fix(ssr): properly handle ssr slot and fallback --- packages/server-renderer/src/helpers/ssrRenderSlot.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/server-renderer/src/helpers/ssrRenderSlot.ts b/packages/server-renderer/src/helpers/ssrRenderSlot.ts index 19aa4ce63b7..260e5a3bb33 100644 --- a/packages/server-renderer/src/helpers/ssrRenderSlot.ts +++ b/packages/server-renderer/src/helpers/ssrRenderSlot.ts @@ -62,7 +62,7 @@ export function ssrRenderSlotInner( parentComponent, slotScopeId ? ' ' + slotScopeId : '', ) - if (isArray(ret)) { + if (isArray(ret) && slotBuffer.length === 0) { const validSlotContent = ensureValidVNode(ret) if (validSlotContent) { // normal slot From 659c0bfc908605b078cd852a167edabeb4280c9f Mon Sep 17 00:00:00 2001 From: daiwei Date: Wed, 20 Nov 2024 10:20:43 +0800 Subject: [PATCH 2/3] test: add test case --- .../server-renderer/__tests__/ssrSlot.spec.ts | 34 ++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/packages/server-renderer/__tests__/ssrSlot.spec.ts b/packages/server-renderer/__tests__/ssrSlot.spec.ts index 02872274ab6..4cc7fd97ef2 100644 --- a/packages/server-renderer/__tests__/ssrSlot.spec.ts +++ b/packages/server-renderer/__tests__/ssrSlot.spec.ts @@ -1,4 +1,4 @@ -import { createApp } from 'vue' +import { createApp, defineAsyncComponent, h } from 'vue' import { renderToString } from '../src/renderToString' const components = { @@ -154,6 +154,38 @@ describe('ssr: slot', () => { ).toBe(`

1

2

`) }) + // #12438 + test('async component slot with v-if true', async () => { + const Layout = defineAsyncComponent(() => + Promise.resolve({ + template: `
default header
`, + }), + ) + const LayoutLoader = { + setup(_: any, context: any) { + return () => h(Layout, {}, context.slots) + }, + } + expect( + await renderToString( + createApp({ + components: { + LayoutLoader, + }, + template: ` + + + + + + `, + }), + ), + ).toBe(`
new header
`) + }) + // #11326 test('dynamic component slot', async () => { expect( From 413c9239b1751498364d2ff7e3e0cdd642ede377 Mon Sep 17 00:00:00 2001 From: daiwei Date: Wed, 20 Nov 2024 21:37:51 +0800 Subject: [PATCH 3/3] chore: update --- packages/runtime-core/src/component.ts | 2 +- packages/server-renderer/src/helpers/ssrRenderSlot.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/runtime-core/src/component.ts b/packages/runtime-core/src/component.ts index 3ed42ed0b55..5b094a0d611 100644 --- a/packages/runtime-core/src/component.ts +++ b/packages/runtime-core/src/component.ts @@ -806,7 +806,7 @@ export function setupComponent( const { props, children } = instance.vnode const isStateful = isStatefulComponent(instance) initProps(instance, props, isStateful, isSSR) - initSlots(instance, children, optimized) + initSlots(instance, children, optimized || isSSR) const setupResult = isStateful ? setupStatefulComponent(instance, isSSR) diff --git a/packages/server-renderer/src/helpers/ssrRenderSlot.ts b/packages/server-renderer/src/helpers/ssrRenderSlot.ts index 260e5a3bb33..19aa4ce63b7 100644 --- a/packages/server-renderer/src/helpers/ssrRenderSlot.ts +++ b/packages/server-renderer/src/helpers/ssrRenderSlot.ts @@ -62,7 +62,7 @@ export function ssrRenderSlotInner( parentComponent, slotScopeId ? ' ' + slotScopeId : '', ) - if (isArray(ret) && slotBuffer.length === 0) { + if (isArray(ret)) { const validSlotContent = ensureValidVNode(ret) if (validSlotContent) { // normal slot