diff --git a/lib/app/server.js b/lib/app/server.js index 2f8037b4bf..600320f523 100644 --- a/lib/app/server.js +++ b/lib/app/server.js @@ -40,15 +40,14 @@ export default context => { }) context.res.end() } - // Add route to the context - context.route = router.currentRoute // Add meta infos context.meta = _app.$meta() // Error function context.error = _app.$options._nuxt.error.bind(_app) <%= (isDev ? 'const s = isDev && Date.now()' : '') %> - let ctx = getContext(context) + let ctx = null + let componentsLoaded = false let Components = [] let promises = getMatchedComponents(router.match(context.url)).map((Component) => { return new Promise((resolve, reject) => { @@ -67,9 +66,18 @@ export default context => { }) return Promise.all(promises) .then((_Components) => { + componentsLoaded = true Components = _Components // set router's location - router.push(context.url) + return new Promise((resolve) => { + router.push(context.url, resolve) + }) + }) + .then(() => { + // Add route to the context + context.route = router.currentRoute + // Update context + ctx = getContext(context) // nuxtServerInit <% if (store) { %> let promise = (store._actions && store._actions.nuxtServerInit ? store.dispatch('nuxtServerInit', omit(getContext(context), 'redirect', 'error')) : null) @@ -197,9 +205,13 @@ export default context => { return _app }) .catch(function (error) { - if (error && (error instanceof Error || error.constructor.toString().indexOf('Error()') !== -1)) { + if (!componentsLoaded && error instanceof Error) { return Promise.reject(error) } + if (error && (error instanceof Error || error.constructor.toString().indexOf('Error()') !== -1)) { + let statusCode = error.statusCode || error.status || (error.response && error.response.status) || 500 + error = { statusCode, message: error.message } + } else if (typeof error === 'string') { error = { statusCode: 500, message: error } }