From ceae5a88445b972a95a07df8c68cc157227aeccc Mon Sep 17 00:00:00 2001 From: Clark Du Date: Fri, 1 Dec 2017 17:25:21 +0800 Subject: [PATCH] fix(route): error when redirect to different route in router guards --- lib/app/index.js | 17 +++++++++++++++-- lib/app/utils.js | 2 +- test/basic.csr.test.js | 6 ++++++ test/basic.ssr.test.js | 6 ++++++ test/fixtures/basic/pages/router-guard.vue | 11 +++++++++++ 5 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/basic/pages/router-guard.vue diff --git a/lib/app/index.js b/lib/app/index.js index 986ad113ec..65e72d451b 100644 --- a/lib/app/index.js +++ b/lib/app/index.js @@ -8,7 +8,7 @@ import NuxtLink from './components/nuxt-link.js' import NuxtError from '<%= components.ErrorPage ? components.ErrorPage : "./components/nuxt-error.vue" %>' import Nuxt from './components/nuxt.js' import App from '<%= appPath %>' -import { setContext, getLocation } from './utils' +import { setContext, getLocation, getRouteData } from './utils' <% if (store) { %>import { createStore } from './store.js'<% } %> /* Plugins */ @@ -167,7 +167,20 @@ async function createApp (ssrContext) { // If server-side, wait for async component to be resolved first if (process.server && ssrContext && ssrContext.url) { await new Promise((resolve, reject) => { - router.push(ssrContext.url, resolve, reject) + router.push(ssrContext.url, resolve, () => { + let initSSR = true + // navigated to a different route in router guard + router.afterEach(async (to, from, next) => { + if (initSSR) { + ssrContext.url = to.fullPath + app.context.route = await getRouteData(to) + app.context.params = to.params || {} + app.context.query = to.query || {} + } + initSSR = false + resolve() + }) + }) }) } diff --git a/lib/app/utils.js b/lib/app/utils.js index f78973d570..cb2642ced0 100644 --- a/lib/app/utils.js +++ b/lib/app/utils.js @@ -85,7 +85,7 @@ export function resolveRouteComponents(route) { ) } -async function getRouteData(route) { +export async function getRouteData(route) { // Make sure the components are resolved (code-splitting) await resolveRouteComponents(route) // Send back a copy of route with meta based on Component definition diff --git a/test/basic.csr.test.js b/test/basic.csr.test.js index 3c58c37462..83b57293ca 100644 --- a/test/basic.csr.test.js +++ b/test/basic.csr.test.js @@ -179,6 +179,12 @@ test('/fn-midd?please=true', async t => { t.true(h1.includes('Date:')) }) +test('/router-guard', async t => { + await page.nuxt.navigate('/router-guard') + + t.is(await page.$text('p'), 'Nuxt.js') +}) + // Close server and ask nuxt to stop listening to file changes test.after('Closing server and nuxt.js', t => { nuxt.close() diff --git a/test/basic.ssr.test.js b/test/basic.ssr.test.js index 42fca2d4a2..3692ac4ede 100755 --- a/test/basic.ssr.test.js +++ b/test/basic.ssr.test.js @@ -239,6 +239,12 @@ test('/fn-midd?please=true', async t => { t.true(html.includes('

Date:')) }) +test('/router-guard', async t => { + const { html } = await nuxt.renderRoute('/router-guard') + t.true(html.includes('

Nuxt.js

')) + t.false(html.includes('Router Guard')) +}) + // Close server and ask nuxt to stop listening to file changes test.after('Closing server and nuxt.js', t => { nuxt.close() diff --git a/test/fixtures/basic/pages/router-guard.vue b/test/fixtures/basic/pages/router-guard.vue new file mode 100644 index 0000000000..ae5e319c1f --- /dev/null +++ b/test/fixtures/basic/pages/router-guard.vue @@ -0,0 +1,11 @@ + + +