2020-01-24 10:01:56 +08:00
|
|
|
import {
|
|
|
|
|
App,
|
|
|
|
|
Component,
|
|
|
|
|
ComponentInternalInstance,
|
2020-01-24 11:23:10 +08:00
|
|
|
createComponentInstance,
|
|
|
|
|
setupComponent,
|
|
|
|
|
VNode,
|
|
|
|
|
createVNode
|
|
|
|
|
} from 'vue'
|
2020-01-27 05:13:12 +08:00
|
|
|
import { isString, isPromise, isArray } from '@vue/shared'
|
2020-01-24 10:01:56 +08:00
|
|
|
|
2020-01-27 06:35:21 +08:00
|
|
|
export * from './helpers'
|
|
|
|
|
|
2020-01-24 10:01:56 +08:00
|
|
|
type SSRBuffer = SSRBufferItem[]
|
2020-01-27 05:13:12 +08:00
|
|
|
type SSRBufferItem = string | ResolvedSSRBuffer | Promise<SSRBuffer>
|
2020-01-24 10:01:56 +08:00
|
|
|
type ResolvedSSRBuffer = (string | ResolvedSSRBuffer)[]
|
|
|
|
|
|
2020-01-27 05:13:12 +08:00
|
|
|
function createBuffer() {
|
2020-01-24 10:01:56 +08:00
|
|
|
let appendable = false
|
2020-01-27 05:13:12 +08:00
|
|
|
let hasAsync = false
|
2020-01-24 10:01:56 +08:00
|
|
|
const buffer: SSRBuffer = []
|
|
|
|
|
return {
|
|
|
|
|
buffer,
|
2020-01-27 05:13:12 +08:00
|
|
|
hasAsync() {
|
|
|
|
|
return hasAsync
|
|
|
|
|
},
|
2020-01-24 10:01:56 +08:00
|
|
|
push(item: SSRBufferItem) {
|
|
|
|
|
const isStringItem = isString(item)
|
|
|
|
|
if (appendable && isStringItem) {
|
|
|
|
|
buffer[buffer.length - 1] += item as string
|
|
|
|
|
} else {
|
|
|
|
|
buffer.push(item)
|
|
|
|
|
}
|
|
|
|
|
appendable = isStringItem
|
2020-01-27 05:13:12 +08:00
|
|
|
if (!isStringItem && !isArray(item)) {
|
|
|
|
|
// promise
|
|
|
|
|
hasAsync = true
|
|
|
|
|
}
|
2020-01-24 10:01:56 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-27 05:13:12 +08:00
|
|
|
export async function renderToString(app: App): Promise<string> {
|
|
|
|
|
const resolvedBuffer = (await renderComponent(
|
|
|
|
|
app._component,
|
|
|
|
|
app._props
|
|
|
|
|
)) as ResolvedSSRBuffer
|
|
|
|
|
return unrollBuffer(resolvedBuffer)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function renderComponent(
|
2020-01-24 10:01:56 +08:00
|
|
|
comp: Component,
|
2020-01-24 11:23:10 +08:00
|
|
|
props: Record<string, any> | null = null,
|
|
|
|
|
children: VNode['children'] = null,
|
|
|
|
|
parentComponent: ComponentInternalInstance | null = null
|
2020-01-27 05:13:12 +08:00
|
|
|
): ResolvedSSRBuffer | Promise<SSRBuffer> {
|
2020-01-24 11:23:10 +08:00
|
|
|
const vnode = createVNode(comp, props, children)
|
|
|
|
|
const instance = createComponentInstance(vnode, parentComponent)
|
2020-01-27 05:13:12 +08:00
|
|
|
const res = setupComponent(instance, null)
|
|
|
|
|
if (isPromise(res)) {
|
|
|
|
|
return res.then(() => innerRenderComponent(comp, instance))
|
|
|
|
|
} else {
|
|
|
|
|
return innerRenderComponent(comp, instance)
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-24 10:01:56 +08:00
|
|
|
|
2020-01-27 05:13:12 +08:00
|
|
|
function innerRenderComponent(
|
|
|
|
|
comp: Component,
|
|
|
|
|
instance: ComponentInternalInstance
|
|
|
|
|
): ResolvedSSRBuffer | Promise<SSRBuffer> {
|
|
|
|
|
const { buffer, push, hasAsync } = createBuffer()
|
2020-01-24 10:01:56 +08:00
|
|
|
if (typeof comp === 'function') {
|
|
|
|
|
// TODO FunctionalComponent
|
|
|
|
|
} else {
|
|
|
|
|
if (comp.ssrRender) {
|
|
|
|
|
// optimized
|
|
|
|
|
comp.ssrRender(push, instance.proxy)
|
|
|
|
|
} else if (comp.render) {
|
|
|
|
|
// TODO fallback to vdom serialization
|
|
|
|
|
} else {
|
|
|
|
|
// TODO warn component missing render function
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-27 05:13:12 +08:00
|
|
|
// 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.
|
|
|
|
|
return hasAsync()
|
|
|
|
|
? // TS can't figure out the typing due to recursive appearance of Promise
|
|
|
|
|
Promise.all(buffer as any)
|
|
|
|
|
: (buffer as ResolvedSSRBuffer)
|
2019-11-02 10:54:01 +08:00
|
|
|
}
|