mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-30 09:27:13 +00:00
Improve error handling on SSR
This commit is contained in:
parent
d4db5792a4
commit
cfd6b197c4
@ -8,7 +8,7 @@ import { stringify } from 'querystring'
|
|||||||
import { omit } from 'lodash'
|
import { omit } from 'lodash'
|
||||||
import middleware from './middleware'
|
import middleware from './middleware'
|
||||||
import { app, router<%= (store ? ', store' : '') %>, NuxtError } from './index'
|
import { app, router<%= (store ? ', store' : '') %>, NuxtError } from './index'
|
||||||
import { getContext, promiseSeries, promisify, urlJoin } from './utils'
|
import { getMatchedComponents, getContext, promiseSeries, promisify, urlJoin } from './utils'
|
||||||
|
|
||||||
const isDev = <%= isDev %>
|
const isDev = <%= isDev %>
|
||||||
const _app = new Vue(app)
|
const _app = new Vue(app)
|
||||||
@ -40,8 +40,6 @@ export default context => {
|
|||||||
})
|
})
|
||||||
context.res.end()
|
context.res.end()
|
||||||
}
|
}
|
||||||
// set router's location
|
|
||||||
router.push(context.url)
|
|
||||||
// Add route to the context
|
// Add route to the context
|
||||||
context.route = router.currentRoute
|
context.route = router.currentRoute
|
||||||
// Add meta infos
|
// Add meta infos
|
||||||
@ -52,12 +50,26 @@ export default context => {
|
|||||||
<%= (isDev ? 'const s = isDev && Date.now()' : '') %>
|
<%= (isDev ? 'const s = isDev && Date.now()' : '') %>
|
||||||
let ctx = getContext(context)
|
let ctx = getContext(context)
|
||||||
let Components = []
|
let Components = []
|
||||||
return new Promise((resolve) => {
|
let promises = getMatchedComponents(router.match(context.url)).map((Component) => {
|
||||||
// Wait for the lazy-routes are loaded
|
return new Promise((resolve, reject) => {
|
||||||
router.onReady(resolve)
|
const _resolve = (Component) => {
|
||||||
|
if (!Component.options) {
|
||||||
|
Component = Vue.extend(Component) // fix issue #6
|
||||||
|
Component._Ctor = Component
|
||||||
|
} else {
|
||||||
|
Component._Ctor = Component
|
||||||
|
Component.extendOptions = Component.options
|
||||||
|
}
|
||||||
|
resolve(Component)
|
||||||
|
}
|
||||||
|
Component().then(_resolve).catch(reject)
|
||||||
})
|
})
|
||||||
.then(() => {
|
})
|
||||||
Components = router.getMatchedComponents()
|
return Promise.all(promises)
|
||||||
|
.then((_Components) => {
|
||||||
|
Components = _Components
|
||||||
|
// set router's location
|
||||||
|
router.push(context.url)
|
||||||
// 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)
|
||||||
@ -186,8 +198,7 @@ export default context => {
|
|||||||
})
|
})
|
||||||
.catch(function (error) {
|
.catch(function (error) {
|
||||||
if (error && (error instanceof Error || error.constructor.toString().indexOf('Error()') !== -1)) {
|
if (error && (error instanceof Error || error.constructor.toString().indexOf('Error()') !== -1)) {
|
||||||
let statusCode = error.statusCode || error.status || (error.response && error.response.status) || 500
|
return Promise.reject(error)
|
||||||
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 }
|
||||||
|
Loading…
Reference in New Issue
Block a user