Skip to content

Commit bd23683

Browse files
committed
fix: Route name missing for routes that have children
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
1 parent e547639 commit bd23683

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

src/helpers/routes.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,13 @@ exports.makeRoutes = (baseRoutes, {
7272
continue
7373
}
7474

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

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

99-
// Only routes without children have name
100-
if (!defaultRoute.children) {
100+
if (name) {
101101
defaultRoute.name = localizedRoute.name + routesNameSeparator + defaultLocaleRouteNameSuffix
102-
} else {
102+
}
103+
104+
if (defaultRoute.children) {
103105
// Recreate child routes with default suffix added
104106
defaultRoute.children = []
105107
for (const childRoute of route.children) {
@@ -109,7 +111,7 @@ exports.makeRoutes = (baseRoutes, {
109111
}
110112

111113
routes.push(defaultRoute)
112-
} else if (isChild && isExtraRouteTree) {
114+
} else if (isChild && isExtraRouteTree && name) {
113115
localizedRoute.name += routesNameSeparator + defaultLocaleRouteNameSuffix
114116
}
115117
}

test/module.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,17 @@ describe('basic', () => {
137137
})
138138
})
139139

140+
test('navigates to dynamic child route and checks path to other locale', async () => {
141+
const window = await nuxt.renderAndGetWindow(url('/dynamicNested/1'))
142+
143+
const body = window.document.querySelector('body')
144+
expect(body.textContent).toContain('Category')
145+
expect(body.textContent).not.toContain('Subcategory')
146+
147+
// Will only work if navigated-to route has a name.
148+
expect(window.$nuxt.switchLocalePath('fr')).toBe('/fr/imbrication-dynamique/1')
149+
})
150+
140151
test('/dynamicNested/1/2/3 contains link to /fr/imbrication-dynamique/1/2/3', async () => {
141152
const html = await get('/dynamicNested/1/2/3')
142153
expect(cleanUpScripts(html)).toMatchSnapshot()

0 commit comments

Comments
 (0)