Skip to content

Commit

Permalink
fix: NavigationDuplicated error on loading 404 page in SPA (nuxt-modu…
Browse files Browse the repository at this point in the history
…les#705)

Also fixed an issue where would not respect query params when redirecting.

Resolves nuxt-modules#702
  • Loading branch information
rchl authored May 10, 2020
1 parent 138352d commit 6bd80da
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 13 deletions.
25 changes: 13 additions & 12 deletions src/templates/plugin.main.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ export default async (context) => {

await syncVuex(store, newLocale, app.i18n.getLocaleMessage(newLocale), { vuex })

const redirectPath = getRedirectPathForLocale(newLocale)
// Must retrieve from context as it might have changed since plugin initialization.
const { route } = context
const redirectPath = getRedirectPathForLocale(route, newLocale)

if (initialSetup) {
// Redirect will be delayed until middleware runs as redirecting from plugin does not
Expand All @@ -100,25 +102,24 @@ export default async (context) => {
}
}

const getRedirectPathForLocale = locale => {
const getRedirectPathForLocale = (route, locale) => {
if (!locale || app.i18n.differentDomains || strategy === STRATEGIES.NO_PREFIX) {
return
return ''
}

// Must retrieve from context as it might have changed since plugin initialization.
const { route } = context
const routeLocale = getLocaleFromRoute(route)

if (routeLocale === locale) {
return
if (getLocaleFromRoute(route) === locale) {
return ''
}

// At this point we are left with route that either no or different locale.
// At this point we are left with route that either has no or different locale.
let redirectPath = app.switchLocalePath(locale)

if (!redirectPath) {
// Could be a 404 route in which case we should attemp to find matching route for given locale.
redirectPath = app.localePath(route.path, locale)
// Current route could be 404 in which case attempt to find matching route for given locale.
redirectPath = app.localePath(route.fullPath, locale)
if (redirectPath === route.fullPath) {
return ''
}
}

return redirectPath
Expand Down
45 changes: 44 additions & 1 deletion test/browser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ async function createDefaultBrowser () {
staticServer: false,
extendPage (page) {
return {
navigate: createNavigator(page)
navigate: createNavigator(page),
getRouteFullPath () {
return page.runScript(() => window.$nuxt.$route.fullPath)
}
}
}
})
Expand Down Expand Up @@ -244,6 +247,46 @@ describe(`${browserString} (no fallbackLocale, browser language not supported)`,
})
})

describe(`${browserString} (SPA)`, () => {
let nuxt
let browser
let page

beforeAll(async () => {
const overrides = {
mode: 'spa'
}

nuxt = (await setup(loadConfig(__dirname, 'basic', overrides, { merge: true }))).nuxt
browser = await createDefaultBrowser()
})

afterAll(async () => {
if (browser) {
await browser.close()
}

await nuxt.close()
})

test('renders existing page', async () => {
page = await browser.page(url('/'))
expect(await page.getText('body')).toContain('locale: en')
})

test('renders 404 page', async () => {
page = await browser.page(url('/nopage'))
expect(await page.getText('body')).toContain('page could not be found')
})

test('preserves the URL on 404 page', async () => {
const path = '/nopage?a#h'
page = await browser.page(url(path))
expect(await page.getText('body')).toContain('page could not be found')
expect(await page.getRouteFullPath()).toBe(path)
})
})

describe(`${browserString} (SPA with router in hash mode)`, () => {
let nuxt
let browser
Expand Down

0 comments on commit 6bd80da

Please sign in to comment.