fix(route): error when redirect to different route in router guards

This commit is contained in:
Clark Du 2017-12-01 17:25:21 +08:00
parent 32e67c7481
commit ceae5a8844
5 changed files with 39 additions and 3 deletions

View File

@ -8,7 +8,7 @@ import NuxtLink from './components/nuxt-link.js'
import NuxtError from '<%= components.ErrorPage ? components.ErrorPage : "./components/nuxt-error.vue" %>' import NuxtError from '<%= components.ErrorPage ? components.ErrorPage : "./components/nuxt-error.vue" %>'
import Nuxt from './components/nuxt.js' import Nuxt from './components/nuxt.js'
import App from '<%= appPath %>' import App from '<%= appPath %>'
import { setContext, getLocation } from './utils' import { setContext, getLocation, getRouteData } from './utils'
<% if (store) { %>import { createStore } from './store.js'<% } %> <% if (store) { %>import { createStore } from './store.js'<% } %>
/* Plugins */ /* Plugins */
@ -167,7 +167,20 @@ async function createApp (ssrContext) {
// If server-side, wait for async component to be resolved first // If server-side, wait for async component to be resolved first
if (process.server && ssrContext && ssrContext.url) { if (process.server && ssrContext && ssrContext.url) {
await new Promise((resolve, reject) => { 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()
})
})
}) })
} }

View File

@ -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) // Make sure the components are resolved (code-splitting)
await resolveRouteComponents(route) await resolveRouteComponents(route)
// Send back a copy of route with meta based on Component definition // Send back a copy of route with meta based on Component definition

View File

@ -179,6 +179,12 @@ test('/fn-midd?please=true', async t => {
t.true(h1.includes('Date:')) 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 // Close server and ask nuxt to stop listening to file changes
test.after('Closing server and nuxt.js', t => { test.after('Closing server and nuxt.js', t => {
nuxt.close() nuxt.close()

View File

@ -239,6 +239,12 @@ test('/fn-midd?please=true', async t => {
t.true(html.includes('<h1>Date:')) t.true(html.includes('<h1>Date:'))
}) })
test('/router-guard', async t => {
const { html } = await nuxt.renderRoute('/router-guard')
t.true(html.includes('<p>Nuxt.js</p>'))
t.false(html.includes('Router Guard'))
})
// Close server and ask nuxt to stop listening to file changes // Close server and ask nuxt to stop listening to file changes
test.after('Closing server and nuxt.js', t => { test.after('Closing server and nuxt.js', t => {
nuxt.close() nuxt.close()

View File

@ -0,0 +1,11 @@
<template>
<div>Router Guard</div>
</template>
<script>
export default {
beforeRouteEnter(to, from, next) {
next({path: '/async-data'})
}
}
</script>