fix(vue-renderer): improve ready handling (#4511)

This commit is contained in:
Pooya Parsa 2018-12-10 15:46:05 +03:30 committed by Xin Du (Clark)
parent 6d8aeec81c
commit f0cb654a5d

View File

@ -104,11 +104,24 @@ export default class VueRenderer {
}
async ready() {
if (!this.context.options.dev) {
// Production: Load SSR resources from fs
// -- Development mode --
if (this.context.options.dev) {
this.context.nuxt.hook('build:resources', mfs => this.loadResources(mfs, true))
return
}
// -- Production mode --
// Try once to load SSR resources from fs
await this.loadResources(fs)
// Verify
// Without using`nuxt start` (Programatic, Tests and Generate)
if (!this.context.options._start) {
this.context.nuxt.hook('build:resources', () => this.loadResources(fs))
}
// Verify resources
if (this.context.options._start) {
if (!this.isReady) {
throw new Error(
@ -120,10 +133,6 @@ export default class VueRenderer {
)
}
}
} else {
// Development: Listen on build:resources hook
this.context.nuxt.hook('build:resources', mfs => this.loadResources(mfs, true))
}
}
loadResources(_fs, isMFS = false) {
@ -280,12 +289,16 @@ export default class VueRenderer {
return fn(opts)
}
async renderRoute(url, context = {}) {
async renderRoute(url, context = {}, retries = 5) {
/* istanbul ignore if */
if (!this.isReady) {
if (this.context.options.dev && retries > 0) {
consola.info('Waiting for server resources...')
await waitFor(1000)
return this.renderRoute(url, context)
return this.renderRoute(url, context, retries - 1)
} else {
throw new Error('Server resources are not available!')
}
}
// Log rendered url