2020-01-28 06:23:42 +08:00
|
|
|
import {
|
|
|
|
App,
|
|
|
|
Component,
|
|
|
|
ComponentInternalInstance,
|
|
|
|
VNode,
|
2020-01-29 11:58:02 +08:00
|
|
|
VNodeArrayChildren,
|
2020-01-28 06:23:42 +08:00
|
|
|
createVNode,
|
2020-01-29 07:48:27 +08:00
|
|
|
Text,
|
|
|
|
Comment,
|
|
|
|
Fragment,
|
|
|
|
Portal,
|
|
|
|
ShapeFlags,
|
2020-01-29 11:58:02 +08:00
|
|
|
ssrUtils,
|
2020-02-02 13:05:27 +08:00
|
|
|
Slot,
|
|
|
|
Slots
|
2020-01-28 06:23:42 +08:00
|
|
|
} from 'vue'
|
2020-01-29 07:48:27 +08:00
|
|
|
import {
|
|
|
|
isString,
|
|
|
|
isPromise,
|
|
|
|
isArray,
|
|
|
|
isFunction,
|
2020-02-04 07:16:09 +08:00
|
|
|
isVoidTag,
|
|
|
|
escapeHtml
|
2020-01-29 07:48:27 +08:00
|
|
|
} from '@vue/shared'
|
2020-02-04 07:16:09 +08:00
|
|
|
import { renderAttrs } from './helpers/renderAttrs'
|
2020-01-28 06:23:42 +08:00
|
|
|
|
2020-01-29 11:14:43 +08:00
|
|
|
const {
|
2020-01-30 06:36:06 +08:00
|
|
|
isVNode,
|
2020-01-29 11:14:43 +08:00
|
|
|
createComponentInstance,
|
|
|
|
setupComponent,
|
|
|
|
renderComponentRoot,
|
|
|
|
normalizeVNode
|
|
|
|
} = ssrUtils
|
|
|
|
|
2020-01-28 23:46:13 +08:00
|
|
|
// Each component has a buffer array.
|
|
|
|
// A buffer array can contain one of the following:
|
|
|
|
// - plain string
|
|
|
|
// - A resolved buffer (recursive arrays of strings that can be unrolled
|
|
|
|
// synchronously)
|
|
|
|
// - An async buffer (a Promise that resolves to a resolved buffer)
|
2020-01-28 07:06:37 +08:00
|
|
|
type SSRBuffer = SSRBufferItem[]
|
2020-01-28 23:46:13 +08:00
|
|
|
type SSRBufferItem = string | ResolvedSSRBuffer | Promise<ResolvedSSRBuffer>
|
2020-01-28 07:06:37 +08:00
|
|
|
type ResolvedSSRBuffer = (string | ResolvedSSRBuffer)[]
|
2020-01-29 07:48:27 +08:00
|
|
|
type PushFn = (item: SSRBufferItem) => void
|
2020-01-29 11:58:02 +08:00
|
|
|
type Props = Record<string, unknown>
|
2020-01-28 06:23:42 +08:00
|
|
|
|
|
|
|
function createBuffer() {
|
|
|
|
let appendable = false
|
|
|
|
let hasAsync = false
|
|
|
|
const buffer: SSRBuffer = []
|
|
|
|
return {
|
|
|
|
buffer,
|
|
|
|
hasAsync() {
|
|
|
|
return hasAsync
|
|
|
|
},
|
|
|
|
push(item: SSRBufferItem) {
|
|
|
|
const isStringItem = isString(item)
|
|
|
|
if (appendable && isStringItem) {
|
|
|
|
buffer[buffer.length - 1] += item as string
|
|
|
|
} else {
|
|
|
|
buffer.push(item)
|
|
|
|
}
|
|
|
|
appendable = isStringItem
|
|
|
|
if (!isStringItem && !isArray(item)) {
|
|
|
|
// promise
|
|
|
|
hasAsync = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function unrollBuffer(buffer: ResolvedSSRBuffer): string {
|
|
|
|
let ret = ''
|
|
|
|
for (let i = 0; i < buffer.length; i++) {
|
|
|
|
const item = buffer[i]
|
|
|
|
if (isString(item)) {
|
|
|
|
ret += item
|
|
|
|
} else {
|
|
|
|
ret += unrollBuffer(item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2020-01-30 06:36:06 +08:00
|
|
|
export async function renderToString(input: App | VNode): Promise<string> {
|
2020-01-31 01:20:23 +08:00
|
|
|
let buffer: ResolvedSSRBuffer
|
|
|
|
if (isVNode(input)) {
|
|
|
|
// raw vnode, wrap with component
|
|
|
|
buffer = await renderComponent({ render: () => input })
|
|
|
|
} else {
|
|
|
|
// rendering an app
|
|
|
|
const vnode = createVNode(input._component, input._props)
|
|
|
|
vnode.appContext = input._context
|
|
|
|
buffer = await renderComponentVNode(vnode)
|
|
|
|
}
|
|
|
|
return unrollBuffer(buffer)
|
2020-01-28 06:23:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function renderComponent(
|
2020-01-29 11:58:02 +08:00
|
|
|
comp: Component,
|
2020-01-31 01:09:50 +08:00
|
|
|
props: Props | null = null,
|
2020-02-02 13:05:27 +08:00
|
|
|
children: Slots | SSRSlots | null = null,
|
2020-01-29 11:58:02 +08:00
|
|
|
parentComponent: ComponentInternalInstance | null = null
|
|
|
|
): ResolvedSSRBuffer | Promise<ResolvedSSRBuffer> {
|
|
|
|
return renderComponentVNode(
|
|
|
|
createVNode(comp, props, children),
|
|
|
|
parentComponent
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderComponentVNode(
|
2020-01-29 07:48:27 +08:00
|
|
|
vnode: VNode,
|
2020-01-28 06:23:42 +08:00
|
|
|
parentComponent: ComponentInternalInstance | null = null
|
2020-01-28 23:46:13 +08:00
|
|
|
): ResolvedSSRBuffer | Promise<ResolvedSSRBuffer> {
|
2020-01-28 06:23:42 +08:00
|
|
|
const instance = createComponentInstance(vnode, parentComponent)
|
2020-01-29 22:49:17 +08:00
|
|
|
const res = setupComponent(
|
|
|
|
instance,
|
2020-01-30 04:10:45 +08:00
|
|
|
null /* parentSuspense (no need to track for SSR) */,
|
2020-01-29 22:49:17 +08:00
|
|
|
true /* isSSR */
|
|
|
|
)
|
2020-01-28 06:23:42 +08:00
|
|
|
if (isPromise(res)) {
|
2020-01-29 11:58:02 +08:00
|
|
|
return res.then(() => renderComponentSubTree(instance))
|
2020-01-28 06:23:42 +08:00
|
|
|
} else {
|
2020-01-29 11:58:02 +08:00
|
|
|
return renderComponentSubTree(instance)
|
2020-01-28 06:23:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-29 11:58:02 +08:00
|
|
|
function renderComponentSubTree(
|
2020-01-28 06:23:42 +08:00
|
|
|
instance: ComponentInternalInstance
|
2020-01-28 23:46:13 +08:00
|
|
|
): ResolvedSSRBuffer | Promise<ResolvedSSRBuffer> {
|
2020-01-29 07:48:27 +08:00
|
|
|
const comp = instance.type as Component
|
2020-01-28 06:23:42 +08:00
|
|
|
const { buffer, push, hasAsync } = createBuffer()
|
|
|
|
if (isFunction(comp)) {
|
2020-01-29 07:48:27 +08:00
|
|
|
renderVNode(push, renderComponentRoot(instance), instance)
|
2020-01-28 06:23:42 +08:00
|
|
|
} else {
|
|
|
|
if (comp.ssrRender) {
|
|
|
|
// optimized
|
2020-01-30 05:46:18 +08:00
|
|
|
comp.ssrRender(instance.proxy, push, instance)
|
2020-01-28 06:23:42 +08:00
|
|
|
} else if (comp.render) {
|
2020-01-29 07:48:27 +08:00
|
|
|
renderVNode(push, renderComponentRoot(instance), instance)
|
2020-01-28 06:23:42 +08:00
|
|
|
} else {
|
|
|
|
// TODO on the fly template compilation support
|
|
|
|
throw new Error(
|
|
|
|
`Component ${
|
|
|
|
comp.name ? `${comp.name} ` : ``
|
|
|
|
} is missing render function.`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If the current component's buffer contains any Promise from async children,
|
|
|
|
// then it must return a Promise too. Otherwise this is a component that
|
|
|
|
// contains only sync children so we can avoid the async book-keeping overhead.
|
2020-01-28 23:46:13 +08:00
|
|
|
return hasAsync() ? Promise.all(buffer) : (buffer as ResolvedSSRBuffer)
|
2020-01-28 06:23:42 +08:00
|
|
|
}
|
2020-01-28 07:06:37 +08:00
|
|
|
|
2020-01-30 06:36:06 +08:00
|
|
|
function renderVNode(
|
2020-01-29 07:48:27 +08:00
|
|
|
push: PushFn,
|
|
|
|
vnode: VNode,
|
|
|
|
parentComponent: ComponentInternalInstance | null = null
|
|
|
|
) {
|
|
|
|
const { type, shapeFlag, children } = vnode
|
|
|
|
switch (type) {
|
|
|
|
case Text:
|
|
|
|
push(children as string)
|
|
|
|
break
|
|
|
|
case Comment:
|
|
|
|
push(children ? `<!--${children}-->` : `<!---->`)
|
|
|
|
break
|
|
|
|
case Fragment:
|
|
|
|
push(`<!---->`)
|
2020-01-29 11:58:02 +08:00
|
|
|
renderVNodeChildren(push, children as VNodeArrayChildren, parentComponent)
|
2020-01-29 07:48:27 +08:00
|
|
|
push(`<!---->`)
|
|
|
|
break
|
|
|
|
case Portal:
|
|
|
|
// TODO
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
if (shapeFlag & ShapeFlags.ELEMENT) {
|
|
|
|
renderElement(push, vnode, parentComponent)
|
|
|
|
} else if (shapeFlag & ShapeFlags.COMPONENT) {
|
2020-01-29 11:58:02 +08:00
|
|
|
push(renderComponentVNode(vnode, parentComponent))
|
2020-01-29 07:48:27 +08:00
|
|
|
} else if (shapeFlag & ShapeFlags.SUSPENSE) {
|
|
|
|
// TODO
|
|
|
|
} else {
|
|
|
|
console.warn(
|
|
|
|
'[@vue/server-renderer] Invalid VNode type:',
|
|
|
|
type,
|
|
|
|
`(${typeof type})`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderVNodeChildren(
|
|
|
|
push: PushFn,
|
2020-01-29 11:58:02 +08:00
|
|
|
children: VNodeArrayChildren,
|
2020-01-29 07:48:27 +08:00
|
|
|
parentComponent: ComponentInternalInstance | null = null
|
|
|
|
) {
|
|
|
|
for (let i = 0; i < children.length; i++) {
|
|
|
|
renderVNode(push, normalizeVNode(children[i]), parentComponent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderElement(
|
|
|
|
push: PushFn,
|
|
|
|
vnode: VNode,
|
|
|
|
parentComponent: ComponentInternalInstance | null = null
|
|
|
|
) {
|
|
|
|
const tag = vnode.type as string
|
|
|
|
const { props, children, shapeFlag, scopeId } = vnode
|
|
|
|
let openTag = `<${tag}`
|
|
|
|
|
|
|
|
// TODO directives
|
|
|
|
|
|
|
|
if (props !== null) {
|
2020-02-04 07:16:09 +08:00
|
|
|
openTag += renderAttrs(props, tag)
|
2020-01-29 07:48:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (scopeId !== null) {
|
|
|
|
openTag += ` ${scopeId}`
|
|
|
|
const treeOwnerId = parentComponent && parentComponent.type.__scopeId
|
|
|
|
// vnode's own scopeId and the current rendering component's scopeId is
|
|
|
|
// different - this is a slot content node.
|
|
|
|
if (treeOwnerId != null && treeOwnerId !== scopeId) {
|
2020-01-30 10:13:34 +08:00
|
|
|
openTag += ` ${treeOwnerId}-s`
|
2020-01-29 07:48:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
push(openTag + `>`)
|
|
|
|
if (!isVoidTag(tag)) {
|
|
|
|
let hasChildrenOverride = false
|
|
|
|
if (props !== null) {
|
|
|
|
if (props.innerHTML) {
|
|
|
|
hasChildrenOverride = true
|
|
|
|
push(props.innerHTML)
|
|
|
|
} else if (props.textContent) {
|
|
|
|
hasChildrenOverride = true
|
2020-01-30 04:10:45 +08:00
|
|
|
push(escapeHtml(props.textContent))
|
2020-01-29 07:48:27 +08:00
|
|
|
} else if (tag === 'textarea' && props.value) {
|
|
|
|
hasChildrenOverride = true
|
2020-01-30 04:10:45 +08:00
|
|
|
push(escapeHtml(props.value))
|
2020-01-29 07:48:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!hasChildrenOverride) {
|
|
|
|
if (shapeFlag & ShapeFlags.TEXT_CHILDREN) {
|
2020-01-30 04:10:45 +08:00
|
|
|
push(escapeHtml(children as string))
|
2020-01-29 07:48:27 +08:00
|
|
|
} else if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
|
2020-01-29 11:58:02 +08:00
|
|
|
renderVNodeChildren(
|
|
|
|
push,
|
|
|
|
children as VNodeArrayChildren,
|
|
|
|
parentComponent
|
|
|
|
)
|
2020-01-29 07:48:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
push(`</${tag}>`)
|
|
|
|
}
|
2020-01-28 07:06:37 +08:00
|
|
|
}
|
|
|
|
|
2020-02-02 13:05:27 +08:00
|
|
|
export type SSRSlots = Record<string, SSRSlot>
|
|
|
|
|
|
|
|
export type SSRSlot = (
|
2020-01-29 11:58:02 +08:00
|
|
|
props: Props,
|
|
|
|
push: PushFn,
|
|
|
|
parentComponent: ComponentInternalInstance | null
|
|
|
|
) => void
|
|
|
|
|
|
|
|
export function renderSlot(
|
2020-02-02 13:05:27 +08:00
|
|
|
slotFn: Slot | SSRSlot,
|
2020-01-29 11:58:02 +08:00
|
|
|
slotProps: Props,
|
|
|
|
push: PushFn,
|
|
|
|
parentComponent: ComponentInternalInstance | null = null
|
|
|
|
) {
|
|
|
|
// template-compiled slots are always rendered as fragments
|
|
|
|
push(`<!---->`)
|
2020-01-30 05:46:18 +08:00
|
|
|
if (slotFn.length > 1) {
|
|
|
|
// only ssr-optimized slot fns accept more than 1 arguments
|
2020-01-29 11:58:02 +08:00
|
|
|
slotFn(slotProps, push, parentComponent)
|
|
|
|
} else {
|
|
|
|
// normal slot
|
|
|
|
renderVNodeChildren(push, (slotFn as Slot)(slotProps), parentComponent)
|
|
|
|
}
|
|
|
|
push(`<!---->`)
|
2020-01-28 07:06:37 +08:00
|
|
|
}
|