Fix updated route in context + error handling

This commit is contained in:
Sébastien Chopin 2017-03-24 15:49:24 +01:00
parent a13be619e2
commit 52d1df7a24

View File

@ -40,15 +40,14 @@ export default context => {
}) })
context.res.end() context.res.end()
} }
// Add route to the context
context.route = router.currentRoute
// Add meta infos // Add meta infos
context.meta = _app.$meta() context.meta = _app.$meta()
// Error function // Error function
context.error = _app.$options._nuxt.error.bind(_app) context.error = _app.$options._nuxt.error.bind(_app)
<%= (isDev ? 'const s = isDev && Date.now()' : '') %> <%= (isDev ? 'const s = isDev && Date.now()' : '') %>
let ctx = getContext(context) let ctx = null
let componentsLoaded = false
let Components = [] let Components = []
let promises = getMatchedComponents(router.match(context.url)).map((Component) => { let promises = getMatchedComponents(router.match(context.url)).map((Component) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@ -67,9 +66,18 @@ export default context => {
}) })
return Promise.all(promises) return Promise.all(promises)
.then((_Components) => { .then((_Components) => {
componentsLoaded = true
Components = _Components Components = _Components
// set router's location // 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 // nuxtServerInit
<% if (store) { %> <% if (store) { %>
let promise = (store._actions && store._actions.nuxtServerInit ? store.dispatch('nuxtServerInit', omit(getContext(context), 'redirect', 'error')) : null) 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 return _app
}) })
.catch(function (error) { .catch(function (error) {
if (error && (error instanceof Error || error.constructor.toString().indexOf('Error()') !== -1)) { if (!componentsLoaded && error instanceof Error) {
return Promise.reject(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') { else if (typeof error === 'string') {
error = { statusCode: 500, message: error } error = { statusCode: 500, message: error }
} }