Skip to content

Commit 1fe877f

Browse files
committed
feat: ssrPrefetch option
1 parent d483a49 commit 1fe877f

File tree

1 file changed

+35
-8
lines changed

1 file changed

+35
-8
lines changed

src/server/render.js

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ let warned = Object.create(null)
1818
const warnOnce = msg => {
1919
if (!warned[msg]) {
2020
warned[msg] = true
21+
// eslint-disable-next-line no-console
2122
console.warn(`\n\u001b[31m${msg}\u001b[39m\n`)
2223
}
2324
}
@@ -48,6 +49,21 @@ const normalizeRender = vm => {
4849
}
4950
}
5051

52+
function waitForSsrPrefetch (vm, resolve, reject) {
53+
if (isDef(vm.$options.ssrPrefetch)) {
54+
try {
55+
const result = vm.$options.ssrPrefetch.call(vm, vm)
56+
if (result && typeof result.then === 'function') {
57+
result.then(resolve).catch(reject)
58+
return
59+
}
60+
} catch (e) {
61+
reject(e)
62+
}
63+
}
64+
resolve()
65+
}
66+
5167
function renderNode (node, isRoot, context) {
5268
if (node.isString) {
5369
renderStringNode(node, context)
@@ -165,13 +181,20 @@ function renderComponentInner (node, isRoot, context) {
165181
context.activeInstance
166182
)
167183
normalizeRender(child)
168-
const childNode = child._render()
169-
childNode.parent = node
170-
context.renderStates.push({
171-
type: 'Component',
172-
prevActive
173-
})
174-
renderNode(childNode, isRoot, context)
184+
185+
const resolve = () => {
186+
const childNode = child._render()
187+
childNode.parent = node
188+
context.renderStates.push({
189+
type: 'Component',
190+
prevActive
191+
})
192+
renderNode(childNode, isRoot, context)
193+
}
194+
195+
const reject = context.done
196+
197+
waitForSsrPrefetch(child, resolve, reject)
175198
}
176199

177200
function renderAsyncComponent (node, isRoot, context) {
@@ -391,6 +414,10 @@ export function createRenderFunction (
391414
})
392415
installSSRHelpers(component)
393416
normalizeRender(component)
394-
renderNode(component._render(), true, context)
417+
418+
const resolve = () => {
419+
renderNode(component._render(), true, context)
420+
}
421+
waitForSsrPrefetch(component, resolve, done)
395422
}
396423
}

0 commit comments

Comments
 (0)