Skip to content

Commit

Permalink
fix: Route name missing for routes that have children
Browse files Browse the repository at this point in the history
With page structure like

```
pages/posts.vue
pages/posts/extra.vue
```

`posts.vue` is a parent route and `extra.vue` is a child route.
Former has `posts` name and latter has `posts_extra` name.
Removing name from parent route makes parent route inaccessible
through name and thus breaks internal functions like `localePath`
and `switchLocalePath`.

Fix by not removing names from routes.

And also checking if name exists at all before trying to add locale
as with some file structures, for example:

```
pages/posts.vue
pages/posts/index.vue
```

the parent `posts.vue` route has no name as `posts` name is assigned
to child and child is shown by default when going to /posts.
Trying to add locale in that case ended up with route named
`undefined_xx`.

Resolves #356
  • Loading branch information
rchl committed Sep 26, 2019
1 parent e547639 commit bd23683
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/helpers/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,13 @@ exports.makeRoutes = (baseRoutes, {
continue
}

// Make localized route name
localizedRoute.name = name + routesNameSeparator + locale
// Make localized route name. Name might not exist on parent route if child has same path.
if (name) {
localizedRoute.name = name + routesNameSeparator + locale
}

// Generate localized children routes if any
if (route.children) {
delete localizedRoute.name
localizedRoute.children = []
for (let i = 0, length1 = route.children.length; i < length1; i++) {
localizedRoute.children = localizedRoute.children.concat(buildLocalizedRoutes(route.children[i], { locales: [locale] }, true, isExtraRouteTree))
Expand All @@ -96,10 +97,11 @@ exports.makeRoutes = (baseRoutes, {
if (!isChild) {
const defaultRoute = { ...localizedRoute, path }

// Only routes without children have name
if (!defaultRoute.children) {
if (name) {
defaultRoute.name = localizedRoute.name + routesNameSeparator + defaultLocaleRouteNameSuffix
} else {
}

if (defaultRoute.children) {
// Recreate child routes with default suffix added
defaultRoute.children = []
for (const childRoute of route.children) {
Expand All @@ -109,7 +111,7 @@ exports.makeRoutes = (baseRoutes, {
}

routes.push(defaultRoute)
} else if (isChild && isExtraRouteTree) {
} else if (isChild && isExtraRouteTree && name) {
localizedRoute.name += routesNameSeparator + defaultLocaleRouteNameSuffix
}
}
Expand Down
11 changes: 11 additions & 0 deletions test/module.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,17 @@ describe('basic', () => {
})
})

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

const body = window.document.querySelector('body')
expect(body.textContent).toContain('Category')
expect(body.textContent).not.toContain('Subcategory')

// Will only work if navigated-to route has a name.
expect(window.$nuxt.switchLocalePath('fr')).toBe('/fr/imbrication-dynamique/1')
})

test('/dynamicNested/1/2/3 contains link to /fr/imbrication-dynamique/1/2/3', async () => {
const html = await get('/dynamicNested/1/2/3')
expect(cleanUpScripts(html)).toMatchSnapshot()
Expand Down

0 comments on commit bd23683

Please sign in to comment.