wip(ssr): should apply app context when rendering app instance
This commit is contained in:
parent
5c2fe536dc
commit
c02e7bc7d6
@ -24,6 +24,7 @@ export interface App<HostElement = any> {
|
||||
_component: Component
|
||||
_props: Data | null
|
||||
_container: HostElement | null
|
||||
_context: AppContext
|
||||
}
|
||||
|
||||
export interface AppConfig {
|
||||
@ -103,6 +104,7 @@ export function createAppAPI<HostNode, HostElement>(
|
||||
_component: rootComponent,
|
||||
_props: rootProps,
|
||||
_container: null,
|
||||
_context: context,
|
||||
|
||||
get config() {
|
||||
return context.config
|
||||
|
@ -1,7 +1,28 @@
|
||||
import { createApp, h, createCommentVNode, withScopeId } from 'vue'
|
||||
import {
|
||||
createApp,
|
||||
h,
|
||||
createCommentVNode,
|
||||
withScopeId,
|
||||
resolveComponent,
|
||||
ComponentOptions
|
||||
} from 'vue'
|
||||
import { renderToString, renderComponent, renderSlot, escapeHtml } from '../src'
|
||||
|
||||
describe('ssr: renderToString', () => {
|
||||
test('should apply app context', async () => {
|
||||
const app = createApp({
|
||||
render() {
|
||||
const Foo = resolveComponent('foo') as ComponentOptions
|
||||
return h(Foo)
|
||||
}
|
||||
})
|
||||
app.component('foo', {
|
||||
render: () => h('div', 'foo')
|
||||
})
|
||||
const html = await renderToString(app)
|
||||
expect(html).toBe(`<div>foo</div>`)
|
||||
})
|
||||
|
||||
describe('components', () => {
|
||||
test('vnode components', async () => {
|
||||
expect(
|
||||
|
@ -83,10 +83,17 @@ function unrollBuffer(buffer: ResolvedSSRBuffer): string {
|
||||
}
|
||||
|
||||
export async function renderToString(input: App | VNode): Promise<string> {
|
||||
const resolvedBuffer = await (isVNode(input)
|
||||
? renderComponent({ render: () => input })
|
||||
: renderComponent(input._component, input._props))
|
||||
return unrollBuffer(resolvedBuffer)
|
||||
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)
|
||||
}
|
||||
|
||||
export function renderComponent(
|
||||
|
Loading…
x
Reference in New Issue
Block a user