mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-23 14:15:13 +00:00
fix(route): error when redirect to different route in router guards
This commit is contained in:
parent
32e67c7481
commit
ceae5a8844
@ -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()
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
@ -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()
|
||||
|
@ -239,6 +239,12 @@ test('/fn-midd?please=true', async t => {
|
||||
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
|
||||
test.after('Closing server and nuxt.js', t => {
|
||||
nuxt.close()
|
||||
|
11
test/fixtures/basic/pages/router-guard.vue
vendored
Normal file
11
test/fixtures/basic/pages/router-guard.vue
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
<template>
|
||||
<div>Router Guard</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
beforeRouteEnter(to, from, next) {
|
||||
next({path: '/async-data'})
|
||||
}
|
||||
}
|
||||
</script>
|
Loading…
Reference in New Issue
Block a user