2020-01-28 06:23:42 +08:00
|
|
|
import {
|
|
|
|
type App,
|
2023-12-26 19:39:47 +08:00
|
|
|
type VNode,
|
2020-02-19 02:26:15 +08:00
|
|
|
createApp,
|
2020-06-26 23:09:47 +08:00
|
|
|
createVNode,
|
2020-03-17 03:38:35 +08:00
|
|
|
ssrContextKey,
|
2020-06-26 23:09:47 +08:00
|
|
|
ssrUtils,
|
2020-01-28 06:23:42 +08:00
|
|
|
} from 'vue'
|
2020-06-26 23:09:47 +08:00
|
|
|
import { isPromise, isString } from '@vue/shared'
|
|
|
|
import { type SSRBuffer, type SSRContext, renderComponentVNode } from './render'
|
2020-02-16 06:41:20 +08:00
|
|
|
|
2020-06-26 23:09:47 +08:00
|
|
|
const { isVNode } = ssrUtils
|
2020-01-28 06:23:42 +08:00
|
|
|
|
2020-06-26 23:09:47 +08:00
|
|
|
async function unrollBuffer(buffer: SSRBuffer): Promise<string> {
|
2020-06-26 23:10:30 +08:00
|
|
|
if (buffer.hasAsync) {
|
|
|
|
let ret = ''
|
|
|
|
for (let i = 0; i < buffer.length; i++) {
|
|
|
|
let item = buffer[i]
|
|
|
|
if (isPromise(item)) {
|
|
|
|
item = await item
|
|
|
|
}
|
|
|
|
if (isString(item)) {
|
|
|
|
ret += item
|
|
|
|
} else {
|
|
|
|
ret += await unrollBuffer(item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
} else {
|
|
|
|
// sync buffer can be more efficiently unrolled without unnecessary await
|
|
|
|
// ticks
|
|
|
|
return unrollBufferSync(buffer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function unrollBufferSync(buffer: SSRBuffer): string {
|
2020-01-28 06:23:42 +08:00
|
|
|
let ret = ''
|
|
|
|
for (let i = 0; i < buffer.length; i++) {
|
2020-06-26 23:09:47 +08:00
|
|
|
let item = buffer[i]
|
2020-01-28 06:23:42 +08:00
|
|
|
if (isString(item)) {
|
|
|
|
ret += item
|
|
|
|
} else {
|
2020-06-26 23:10:30 +08:00
|
|
|
// since this is a sync buffer, child buffers are never promises
|
|
|
|
ret += unrollBufferSync(item as SSRBuffer)
|
2020-01-28 06:23:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2020-02-16 06:41:20 +08:00
|
|
|
export async function renderToString(
|
|
|
|
input: App | VNode,
|
|
|
|
context: SSRContext = {},
|
|
|
|
): Promise<string> {
|
2020-01-31 01:20:23 +08:00
|
|
|
if (isVNode(input)) {
|
2020-02-16 06:41:20 +08:00
|
|
|
// raw vnode, wrap with app (for context)
|
|
|
|
return renderToString(createApp({ render: () => input }), context)
|
2020-01-31 01:20:23 +08:00
|
|
|
}
|
2020-02-16 06:41:20 +08:00
|
|
|
|
2020-02-25 00:23:35 +08:00
|
|
|
// rendering an app
|
|
|
|
const vnode = createVNode(input._component, input._props)
|
|
|
|
vnode.appContext = input._context
|
|
|
|
// provide the ssr context to the tree
|
|
|
|
input.provide(ssrContextKey, context)
|
|
|
|
const buffer = await renderComponentVNode(vnode)
|
|
|
|
|
2022-05-13 15:57:10 +08:00
|
|
|
const result = await unrollBuffer(buffer as SSRBuffer)
|
|
|
|
|
2020-03-31 22:52:42 +08:00
|
|
|
await resolveTeleports(context)
|
2020-02-16 06:41:20 +08:00
|
|
|
|
2022-10-26 18:30:15 +08:00
|
|
|
if (context.__watcherHandles) {
|
|
|
|
for (const unwatch of context.__watcherHandles) {
|
|
|
|
unwatch()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-13 15:57:10 +08:00
|
|
|
return result
|
2020-02-25 00:23:35 +08:00
|
|
|
}
|
|
|
|
|
2022-05-17 12:41:40 +08:00
|
|
|
export async function resolveTeleports(context: SSRContext) {
|
2020-03-31 22:52:42 +08:00
|
|
|
if (context.__teleportBuffers) {
|
|
|
|
context.teleports = context.teleports || {}
|
|
|
|
for (const key in context.__teleportBuffers) {
|
2020-02-25 00:23:35 +08:00
|
|
|
// note: it's OK to await sequentially here because the Promises were
|
|
|
|
// created eagerly in parallel.
|
2021-07-20 06:24:18 +08:00
|
|
|
context.teleports[key] = await unrollBuffer(
|
2022-07-06 16:27:30 +08:00
|
|
|
await Promise.all([context.__teleportBuffers[key]]),
|
2021-07-20 06:24:18 +08:00
|
|
|
)
|
2020-02-25 00:23:35 +08:00
|
|
|
}
|
|
|
|
}
|
2020-02-16 06:41:20 +08:00
|
|
|
}
|