From e5906e5ac4abab3f29038d61339be872f7bf57c8 Mon Sep 17 00:00:00 2001 From: Sebastien Chopin Date: Fri, 7 Jul 2017 15:44:04 +0200 Subject: [PATCH] Avoid loop redirect on server-side --- lib/app/server.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/app/server.js b/lib/app/server.js index fbddf38aa9..7bcc1eb43a 100644 --- a/lib/app/server.js +++ b/lib/app/server.js @@ -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) -} \ No newline at end of file +}