Skip to content

Commit

Permalink
fix: favor non-prefixed route with prefix_and_default strategy
Browse files Browse the repository at this point in the history
When using "localePath" with a route path argument (rather than route name),
there was a bug with picking prefixed route path for default
route rather than preferred non-prefixed path.

Resolve #721
  • Loading branch information
rchl committed May 25, 2020
1 parent aba92b3 commit e6a2ce4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/templates/plugin.routing.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ function localeRoute (route, locale) {
const localizedRoute = Object.assign({}, route)

if (route.path && !route.name) {
const isDefaultLocale = locale === defaultLocale
// if route has a path defined but no name, resolve full route using the path
const isPrefixed = (
// don't prefix default locale
!(locale === defaultLocale && strategy === STRATEGIES.PREFIX_EXCEPT_DEFAULT) &&
const isPrefixed =
// don't prefix default locale
!(isDefaultLocale && [STRATEGIES.PREFIX_EXCEPT_DEFAULT, STRATEGIES.PREFIX_AND_DEFAULT].includes(strategy)) &&
// no prefix for any language
!(strategy === STRATEGIES.NO_PREFIX) &&
// no prefix for different domains
!i18n.differentDomains
)

const path = (isPrefixed ? `/${locale}${route.path}` : route.path)

Expand Down
9 changes: 8 additions & 1 deletion test/module.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -617,12 +617,19 @@ describe('prefix_and_default strategy', () => {
await expect(get('/fr')).resolves.toContain('page: Accueil')
})

test('localeRoute returns localized route name for default locale', async () => {
test('localeRoute returns localized route (by route name)', async () => {
const window = await nuxt.renderAndGetWindow(url('/'))
expect(window.$nuxt.localeRoute('index', 'en')).toMatchObject({ name: 'index___en___default', fullPath: '/' })
expect(window.$nuxt.localeRoute('index', 'fr')).toMatchObject({ name: 'index___fr', fullPath: '/fr' })
})

test('localeRoute returns localized route (by route path)', async () => {
const window = await nuxt.renderAndGetWindow(url('/'))
// Prefer unprefixed path for default locale:
expect(window.$nuxt.localeRoute('/simple', 'en')).toMatchObject({ name: 'simple___en___default', fullPath: '/simple' })
expect(window.$nuxt.localeRoute('/simple', 'fr')).toMatchObject({ name: 'simple___fr', fullPath: '/fr/simple' })
})

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

0 comments on commit e6a2ce4

Please sign in to comment.