Avoid loop redirect on server-side

This commit is contained in:
Sebastien Chopin 2017-07-07 15:44:04 +02:00
parent b65f4533c6
commit e5906e5ac4

View File

@ -32,6 +32,11 @@ export default async (context) => {
if (opts.path.indexOf('http') !== 0 && ('<%= router.base %>' !== '/' && opts.path.indexOf('<%= router.base %>') !== 0)) {
opts.path = urlJoin('<%= router.base %>', opts.path)
}
// Avoid loop redirect
if (opts.path === context.url) {
context.redirected = false
return
}
context.res.writeHead(opts.status, {
'Location': opts.path
})
@ -74,12 +79,14 @@ export default async (context) => {
}
// nuxtServerInit
<% if (store) { %>
let promise = (store._actions && store._actions.nuxtServerInit ? store.dispatch('nuxtServerInit', omit(getContext(context, app), 'redirect', 'error')) : null)
let promise = (store._actions && store._actions.nuxtServerInit ? store.dispatch('nuxtServerInit', getContext(context, app)) : null)
if (!promise || (!(promise instanceof Promise) && (typeof promise.then !== 'function'))) promise = Promise.resolve()
<% } else { %>
let promise = Promise.resolve()
<% } %>
await promise
// If nuxtServerInit made a redirect
if (context.redirected) return _noopApp
// Call global middleware (nuxt.config.js)
let midd = <%= serialize(router.middleware, { isJSON: true }) %>
midd = midd.map((name) => {
@ -222,4 +229,4 @@ function sanitizeDynamicComponents(components) {
components[name] = component
})
return clone(components)
}
}