refactor(vue-renderer): improve ready status error (#5339)

This commit is contained in:
Pooya Parsa 2019-03-23 11:33:08 +04:30 committed by GitHub
parent caf5198430
commit 535327c4f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -33,6 +33,10 @@ export default class VueRenderer {
spaTemplate: undefined,
errorTemplate: this.parseTemplate('Nuxt.js Internal Server Error')
})
// Default status
this._state = 'created'
this._error = null
}
get assetsMapping() {
@ -120,12 +124,25 @@ export default class VueRenderer {
return context.renderResourceHints()
}
async ready() {
if (this._readyCalled) {
return this
ready() {
if (!this._readyPromise) {
this._state = 'loading'
this._readyPromise = this._ready()
.then(() => {
this._state = 'ready'
return this
})
.catch((error) => {
this._state = 'error'
this._error = error
throw error
})
}
this._readyCalled = true
return this._readyPromise
}
async _ready() {
// Resolve dist path
this.distPath = path.resolve(this.context.options.buildDir, 'dist', 'server')
@ -156,8 +173,6 @@ export default class VueRenderer {
`No build files found in ${this.distPath}.\nUse either \`nuxt build\` or \`builder.build()\` or start nuxt in development mode.`
)
}
return this
}
async loadResources(_fs) {
@ -186,7 +201,6 @@ export default class VueRenderer {
// Skip unavailable resources
if (!resource) {
consola.debug('Resource not available:', resourceName)
continue
}
@ -212,9 +226,6 @@ export default class VueRenderer {
this.createRenderer()
}
// Call resourcesLoaded hook
consola.debug('Resources loaded:', updated.join(','))
return this.context.nuxt.callHook('render:resourcesLoaded', this.context.resources)
}
@ -437,23 +448,23 @@ export default class VueRenderer {
}
}
_throwNotReadyError() {
const error = new Error()
error.statusCode = 500
if (!this._readyCalled) {
error.message = 'Nuxt is not initialized! `nuxt.ready()` should be called.'
} else {
error.message = `SSR renderer is not initialized! Please check ${this.distPath} existence.`
}
throw error
}
async renderRoute(url, context = {}) {
/* istanbul ignore if */
if (!this.isReady) {
// Production
if (!this.context.options.dev) {
return this._throwNotReadyError()
switch (this._state) {
case 'created':
throw new Error('Renderer ready() is not called! Please ensure `nuxt.ready()` is called and awaited.')
case 'loading':
throw new Error(`Renderer is loading.`)
case 'error':
throw this._error
case 'ready':
throw new Error(`Renderer is loaded but not all resources are unavailable! Please check ${this.distPath} existence.`)
default:
throw new Error('Renderer is in unknown state!')
}
}
// Tell nuxt middleware to render UI
return false