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,25 +104,34 @@ export default class VueRenderer {
} }
async ready() { async ready() {
if (!this.context.options.dev) { // -- Development mode --
// Production: Load SSR resources from fs
await this.loadResources(fs)
// Verify if (this.context.options.dev) {
if (this.context.options._start) {
if (!this.isReady) {
throw new Error(
'No build files found. Use either `nuxt build` or `builder.build()` or start nuxt in development mode.'
)
} else if (this.context.options.modern && !this.context.resources.modernManifest) {
throw new Error(
'No modern build files found. Use either `nuxt build --modern` or `modern` option to build modern files.'
)
}
}
} else {
// Development: Listen on build:resources hook
this.context.nuxt.hook('build:resources', mfs => this.loadResources(mfs, true)) 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)
// 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(
'No build files found. Use either `nuxt build` or `builder.build()` or start nuxt in development mode.'
)
} else if (this.context.options.modern && !this.context.resources.modernManifest) {
throw new Error(
'No modern build files found. Use either `nuxt build --modern` or `modern` option to build modern files.'
)
}
} }
} }
@ -280,12 +289,16 @@ export default class VueRenderer {
return fn(opts) return fn(opts)
} }
async renderRoute(url, context = {}) { async renderRoute(url, context = {}, retries = 5) {
/* istanbul ignore if */ /* istanbul ignore if */
if (!this.isReady) { if (!this.isReady) {
consola.info('Waiting for server resources...') if (this.context.options.dev && retries > 0) {
await waitFor(1000) consola.info('Waiting for server resources...')
return this.renderRoute(url, context) await waitFor(1000)
return this.renderRoute(url, context, retries - 1)
} else {
throw new Error('Server resources are not available!')
}
} }
// Log rendered url // Log rendered url