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

38 lines
1.1 KiB
TypeScript
Raw Normal View History

2020-02-06 10:04:40 +08:00
import { Props, PushFn, renderVNodeChildren } from '../renderToString'
import { ComponentInternalInstance, Slot, Slots } from 'vue'
export type SSRSlots = Record<string, SSRSlot>
export type SSRSlot = (
props: Props,
push: PushFn,
2020-02-07 06:45:34 +08:00
parentComponent: ComponentInternalInstance | null,
scopeId: string | null
2020-02-06 10:04:40 +08:00
) => void
2020-02-07 01:07:25 +08:00
export function ssrRenderSlot(
2020-02-06 10:04:40 +08:00
slots: Slots | SSRSlots,
slotName: string,
slotProps: Props,
fallbackRenderFn: (() => void) | null,
push: PushFn,
2020-02-16 06:41:20 +08:00
parentComponent: ComponentInternalInstance
2020-02-06 10:04:40 +08:00
) {
// template-compiled slots are always rendered as fragments
push(`<!--[-->`)
2020-02-06 10:04:40 +08:00
const slotFn = slots[slotName]
if (slotFn) {
if (slotFn.length > 1) {
// only ssr-optimized slot fns accept more than 1 arguments
2020-02-07 06:45:34 +08:00
const scopeId = parentComponent && parentComponent.type.__scopeId
2020-02-07 14:06:51 +08:00
slotFn(slotProps, push, parentComponent, scopeId ? ` ${scopeId}-s` : ``)
2020-02-06 10:04:40 +08:00
} else {
// normal slot
renderVNodeChildren(push, (slotFn as Slot)(slotProps), parentComponent)
}
} else if (fallbackRenderFn) {
fallbackRenderFn()
}
push(`<!--]-->`)
2020-02-06 10:04:40 +08:00
}