refactor(ssr): adjust renderToString implementation

This commit is contained in:
Evan You 2020-01-30 12:09:50 -05:00
parent 842b368097
commit 5c2fe536dc

View File

@ -12,8 +12,7 @@ import {
Portal, Portal,
ShapeFlags, ShapeFlags,
ssrUtils, ssrUtils,
Slot, Slot
createApp
} from 'vue' } from 'vue'
import { import {
isString, isString,
@ -84,22 +83,16 @@ function unrollBuffer(buffer: ResolvedSSRBuffer): string {
} }
export async function renderToString(input: App | VNode): Promise<string> { export async function renderToString(input: App | VNode): Promise<string> {
if (isVNode(input)) { const resolvedBuffer = await (isVNode(input)
return renderAppToString(createApp({ render: () => input })) ? renderComponent({ render: () => input })
} else { : renderComponent(input._component, input._props))
return renderAppToString(input)
}
}
async function renderAppToString(app: App): Promise<string> {
const resolvedBuffer = await renderComponent(app._component, app._props, null)
return unrollBuffer(resolvedBuffer) return unrollBuffer(resolvedBuffer)
} }
export function renderComponent( export function renderComponent(
comp: Component, comp: Component,
props: Props | null, props: Props | null = null,
children: VNodeNormalizedChildren | null, children: VNodeNormalizedChildren | null = null,
parentComponent: ComponentInternalInstance | null = null parentComponent: ComponentInternalInstance | null = null
): ResolvedSSRBuffer | Promise<ResolvedSSRBuffer> { ): ResolvedSSRBuffer | Promise<ResolvedSSRBuffer> {
return renderComponentVNode( return renderComponentVNode(