Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ssr): ssrPrefetch option #9017

Merged
merged 11 commits into from
Dec 20, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: ssrPrefetch option
  • Loading branch information
Akryum committed Nov 3, 2018
commit 1fe877f71f9f0f9185f1e8588dcb0ac3872ee6bf
43 changes: 35 additions & 8 deletions src/server/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ let warned = Object.create(null)
const warnOnce = msg => {
if (!warned[msg]) {
warned[msg] = true
// eslint-disable-next-line no-console
console.warn(`\n\u001b[31m${msg}\u001b[39m\n`)
}
}
Expand Down Expand Up @@ -48,6 +49,21 @@ const normalizeRender = vm => {
}
}

function waitForSsrPrefetch (vm, resolve, reject) {
if (isDef(vm.$options.ssrPrefetch)) {
try {
const result = vm.$options.ssrPrefetch.call(vm, vm)
if (result && typeof result.then === 'function') {
result.then(resolve).catch(reject)
return
}
} catch (e) {
reject(e)
}
}
resolve()
}

function renderNode (node, isRoot, context) {
if (node.isString) {
renderStringNode(node, context)
Expand Down Expand Up @@ -165,13 +181,20 @@ function renderComponentInner (node, isRoot, context) {
context.activeInstance
)
normalizeRender(child)
const childNode = child._render()
childNode.parent = node
context.renderStates.push({
type: 'Component',
prevActive
})
renderNode(childNode, isRoot, context)

const resolve = () => {
const childNode = child._render()
childNode.parent = node
context.renderStates.push({
type: 'Component',
prevActive
})
renderNode(childNode, isRoot, context)
}

const reject = context.done

waitForSsrPrefetch(child, resolve, reject)
}

function renderAsyncComponent (node, isRoot, context) {
Expand Down Expand Up @@ -391,6 +414,10 @@ export function createRenderFunction (
})
installSSRHelpers(component)
normalizeRender(component)
renderNode(component._render(), true, context)

const resolve = () => {
renderNode(component._render(), true, context)
}
waitForSsrPrefetch(component, resolve, done)
}
}