Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support localePath with path input and customized routes #1088

Merged
merged 16 commits into from
Mar 7, 2021
Merged
22 changes: 17 additions & 5 deletions src/templates/plugin.routing.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function localeRoute (route, locale) {
}
}

const localizedRoute = Object.assign({}, route)
let localizedRoute = Object.assign({}, route)

if (route.path && !route.name) {
const isDefaultLocale = locale === defaultLocale
Expand All @@ -61,10 +61,22 @@ function localeRoute (route, locale) {
!(strategy === STRATEGIES.NO_PREFIX) &&
// no prefix for different domains
!i18n.differentDomains

let path = (isPrefixed ? `/${locale}${route.path}` : route.path)
path = path.replace(/\/+$/, '') + (trailingSlash ? '/' : '') || '/'
localizedRoute.path = path
const resolvedRoute = this.router.resolve(route.path).route
const resolvedRouteName = this.getRouteBaseName(resolvedRoute)
if (resolvedRouteName) {
localizedRoute = {
name: getLocaleRouteName(resolvedRouteName, locale),
params: resolvedRoute.params,
query: resolvedRoute.query,
hash: resolvedRoute.hash,
path: route.path
}
} else {
if (isPrefixed) {
localizedRoute.path = `/${locale}${route.path}`
}
localizedRoute.path = localizedRoute.path.replace(/\/+$/, '') + (trailingSlash ? '/' : '') || '/'
}
} else {
if (!route.name && !route.path) {
localizedRoute.name = this.getRouteBaseName()
Expand Down
8 changes: 8 additions & 0 deletions test/browser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,14 @@ describe(`${browserString} (SPA)`, () => {
expect(await (await page.$('body'))?.textContent()).toContain('page could not be found')
expect(await getRouteFullPath(page)).toBe(path)
})

test('preserves the URL on 404 page with non-default locale', async () => {
const path = '/nopage?a#h'
page = await browser.newPage({ locale: 'fr' })
await page.goto(url(path))
expect(await (await page.$('body'))?.textContent()).toContain('page could not be found')
expect(await getRouteFullPath(page)).toBe(`/fr${path}`)
})
})

describe(`${browserString} (SPA with router in hash mode)`, () => {
Expand Down
14 changes: 14 additions & 0 deletions test/module.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,20 @@ describe('prefix_and_default strategy', () => {
expect(window.$nuxt.localeRoute('/simple', 'fr')).toMatchObject({ name: 'simple___fr', fullPath: '/fr/simple' })
})

test('localeRoute returns customized localized route (by route path)', async () => {
const window = await nuxt.renderAndGetWindow(url('/'))
// Prefer unprefixed path for default locale:
expect(window.$nuxt.localeRoute('/about-us', 'en')).toMatchObject({ name: 'about___en___default', fullPath: '/about-us' })
expect(window.$nuxt.localeRoute('/en/about-us', 'en')).toMatchObject({ name: 'about___en___default', fullPath: '/about-us' })
expect(window.$nuxt.localeRoute('/about-us', 'fr')).toMatchObject({ name: 'about___fr', fullPath: '/fr/a-propos' })
expect(window.$nuxt.localeRoute('/about-us?q=1#hash', 'en')).toMatchObject({
name: 'about___en___default',
fullPath: '/about-us?q=1#hash',
query: { q: '1' },
hash: '#hash'
})
})

test('canonical SEO link is added to prefixed default locale', async () => {
const html = await get('/en')
const dom = getDom(html)
Expand Down