2020-02-05 21:04:40 -05: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-06 17:45:34 -05:00
|
|
|
parentComponent: ComponentInternalInstance | null,
|
|
|
|
scopeId: string | null
|
2020-02-05 21:04:40 -05:00
|
|
|
) => void
|
|
|
|
|
2020-02-06 12:07:25 -05:00
|
|
|
export function ssrRenderSlot(
|
2020-02-05 21:04:40 -05:00
|
|
|
slots: Slots | SSRSlots,
|
|
|
|
slotName: string,
|
|
|
|
slotProps: Props,
|
|
|
|
fallbackRenderFn: (() => void) | null,
|
|
|
|
push: PushFn,
|
2020-02-16 01:41:20 +03:00
|
|
|
parentComponent: ComponentInternalInstance
|
2020-02-05 21:04:40 -05:00
|
|
|
) {
|
2020-03-12 22:19:41 -04:00
|
|
|
// template-compiled slots are always rendered as fragments
|
2020-03-13 11:55:04 -04:00
|
|
|
push(`<!--[-->`)
|
2020-02-05 21:04:40 -05:00
|
|
|
const slotFn = slots[slotName]
|
|
|
|
if (slotFn) {
|
|
|
|
if (slotFn.length > 1) {
|
|
|
|
// only ssr-optimized slot fns accept more than 1 arguments
|
2020-02-06 17:45:34 -05:00
|
|
|
const scopeId = parentComponent && parentComponent.type.__scopeId
|
2020-02-07 01:06:51 -05:00
|
|
|
slotFn(slotProps, push, parentComponent, scopeId ? ` ${scopeId}-s` : ``)
|
2020-02-05 21:04:40 -05:00
|
|
|
} else {
|
|
|
|
// normal slot
|
|
|
|
renderVNodeChildren(push, (slotFn as Slot)(slotProps), parentComponent)
|
|
|
|
}
|
|
|
|
} else if (fallbackRenderFn) {
|
|
|
|
fallbackRenderFn()
|
|
|
|
}
|
2020-03-13 11:55:04 -04:00
|
|
|
push(`<!--]-->`)
|
2020-02-05 21:04:40 -05:00
|
|
|
}
|