Skip to content

Commit

Permalink
fix: Locale prefixes missing for child routes with custom paths
Browse files Browse the repository at this point in the history
If one assigned (in component options or pages object) custom, absolute
paths for routes and those routes were child routes, then those routes
didn't get the locale prefix assigned as prefix was never added to
child routes.

That wasn't a problem if route was a top-level route or no custom paths
were assigned as then the path was relative and didn't need locale
prefix to be added.

Fix by also adding locale prefix for routes that are child routes with
absolute path.

Resolves #359
  • Loading branch information
rchl committed Sep 26, 2019
1 parent bd23683 commit 10c1d9d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
6 changes: 4 additions & 2 deletions src/helpers/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,14 @@ exports.makeRoutes = (baseRoutes, {
}
}

const isChildWithRelativePath = isChild && !path.startsWith('/')

// Add route prefix if needed
const shouldAddPrefix = (
// No prefix if app uses different locale domains
!differentDomains &&
// Only add prefix on top level routes
!isChild &&
// No need to add prefix if child's path is relative
!isChildWithRelativePath &&
// Skip default locale if strategy is PREFIX_EXCEPT_DEFAULT
!(locale === defaultLocale && strategy === STRATEGIES.PREFIX_EXCEPT_DEFAULT)
)
Expand Down
22 changes: 11 additions & 11 deletions test/fixture/basic/pages/posts/index.vue
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<template>
<div>
<nuxt-link
exact
:to="localePath({
name: 'posts-slug',
params: {
slug: params[$i18n.locale].slug
}
})">{{ params[$i18n.locale].slug }}</nuxt-link>
</div>
<div>
<nuxt-link
exact
:to="localePath({
name: 'posts-slug',
params: {
slug: params[$i18n.locale].slug
}
})">{{ params[$i18n.locale].slug }}</nuxt-link>
</div>
</template>

<script>
export default {
async asyncData () {
data () {
return {
params: {
en: { slug: 'my-post' },
Expand Down
9 changes: 9 additions & 0 deletions test/module.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,15 @@ describe('basic', () => {
})
})

test('navigates to child route with nameless parent and checks path to other locale', async () => {
const window = await nuxt.renderAndGetWindow(url('/posts'))

const links = window.document.querySelectorAll('a')
expect(links.length).toBe(2)
expect(links[0].getAttribute('href')).toEqual('/fr/articles/')
expect(links[1].getAttribute('href')).toEqual('/posts/my-post')
})

test('navigates to dynamic child route and checks path to other locale', async () => {
const window = await nuxt.renderAndGetWindow(url('/dynamicNested/1'))

Expand Down

0 comments on commit 10c1d9d

Please sign in to comment.