Skip to content

Commit

Permalink
feat(ssr): serverPrefetch
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Sep 2, 2020
1 parent 63f1f18 commit c73b4a0
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/runtime-core/src/componentOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export interface ComponentOptionsBase<
directives?: Record<string, Directive>
inheritAttrs?: boolean
emits?: (E | EE[]) & ThisType<void>
serverPrefetch?(): Promise<any>

// Internal ------------------------------------------------------------------

Expand Down
20 changes: 20 additions & 0 deletions packages/server-renderer/__tests__/renderToStream.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { renderToStream as _renderToStream } from '../src/renderToStream'
import { Readable } from 'stream'
import { ssrRenderSlot } from '../src/helpers/ssrRenderSlot'
import { ssrRenderComponent } from '../src/helpers/ssrRenderComponent'

const promisifyStream = (stream: Readable) => {
return new Promise((resolve, reject) => {
let result = ''
Expand Down Expand Up @@ -599,4 +600,23 @@ describe('ssr: renderToStream', () => {
)
})
})

test('serverPrefetch', async () => {
const msg = Promise.resolve('hello')
const app = createApp({
data() {
return {
msg: ''
}
},
async serverPrefetch() {
this.msg = await msg
},
render() {
return h('div', this.msg)
}
})
const html = await renderToStream(app)
expect(html).toBe(`<div>hello</div>`)
})
})
20 changes: 20 additions & 0 deletions packages/server-renderer/__tests__/renderToString.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { escapeHtml } from '@vue/shared'
import { renderToString } from '../src/renderToString'
import { ssrRenderSlot, SSRSlot } from '../src/helpers/ssrRenderSlot'
import { ssrRenderComponent } from '../src/helpers/ssrRenderComponent'

describe('ssr: renderToString', () => {
test('should apply app context', async () => {
const app = createApp({
Expand Down Expand Up @@ -580,4 +581,23 @@ describe('ssr: renderToString', () => {
).toHaveBeenWarned()
})
})

test('serverPrefetch', async () => {
const msg = Promise.resolve('hello')
const app = createApp({
data() {
return {
msg: ''
}
},
async serverPrefetch() {
this.msg = await msg
},
render() {
return h('div', this.msg)
}
})
const html = await renderToString(app)
expect(html).toBe(`<div>hello</div>`)
})
})
19 changes: 14 additions & 5 deletions packages/server-renderer/src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
Comment,
Component,
ComponentInternalInstance,
ComponentOptions,
DirectiveBinding,
Fragment,
mergeProps,
Expand Down Expand Up @@ -84,12 +85,20 @@ export function renderComponentVNode(
): SSRBuffer | Promise<SSRBuffer> {
const instance = createComponentInstance(vnode, parentComponent, null)
const res = setupComponent(instance, true /* isSSR */)
if (isPromise(res)) {
return res
.catch(err => {
warn(`[@vue/server-renderer]: Uncaught error in async setup:\n`, err)
const hasAsyncSetup = isPromise(res)
const prefetch = (vnode.type as ComponentOptions).serverPrefetch
if (hasAsyncSetup || prefetch) {
let p = hasAsyncSetup
? (res as Promise<void>).catch(err => {
warn(`[@vue/server-renderer]: Uncaught error in async setup:\n`, err)
})
: Promise.resolve()
if (prefetch) {
p = p.then(() => prefetch.call(instance.proxy)).catch(err => {
warn(`[@vue/server-renderer]: Uncaught error in serverPrefetch:\n`, err)
})
.then(() => renderComponentSubTree(instance))
}
return p.then(() => renderComponentSubTree(instance))
} else {
return renderComponentSubTree(instance)
}
Expand Down

0 comments on commit c73b4a0

Please sign in to comment.