From 96cfe01973b01fa4da4892f9a7e99bc8077ad5ac Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 1 Aug 2022 08:51:46 +0100 Subject: [PATCH] fix(nuxt): do not allow catchalls to have child routes (#6257) --- packages/nuxt/src/pages/utils.ts | 3 ++- packages/nuxt/test/pages.test.ts | 42 ++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/packages/nuxt/src/pages/utils.ts b/packages/nuxt/src/pages/utils.ts index 719f2cd6cd..c63d163427 100644 --- a/packages/nuxt/src/pages/utils.ts +++ b/packages/nuxt/src/pages/utils.ts @@ -74,7 +74,8 @@ export function generateRoutesFromFiles (files: string[], pagesDir: string): Nux route.name += (route.name && '-') + segmentName // ex: parent.vue + parent/child.vue - const child = parent.find(parentRoute => parentRoute.name === route.name) + const child = parent.find(parentRoute => parentRoute.name === route.name && !parentRoute.path.endsWith('(.*)*')) + if (child) { parent = child.children route.path = '' diff --git a/packages/nuxt/test/pages.test.ts b/packages/nuxt/test/pages.test.ts index 9bf0b0a2af..9882d302d2 100644 --- a/packages/nuxt/test/pages.test.ts +++ b/packages/nuxt/test/pages.test.ts @@ -67,6 +67,48 @@ describe('pages:generateRoutesFromFiles', () => { } ] }, + { + description: 'should generate correct id for catchall (order 1)', + files: [ + `${pagesDir}/[...stories].vue`, + `${pagesDir}/stories/[id].vue` + ], + output: [ + { + name: 'stories', + path: '/:stories(.*)*', + file: `${pagesDir}/[...stories].vue`, + children: [] + }, + { + name: 'stories-id', + path: '/stories/:id', + file: `${pagesDir}/stories/[id].vue`, + children: [] + } + ] + }, + { + description: 'should generate correct id for catchall (order 2)', + files: [ + `${pagesDir}/stories/[id].vue`, + `${pagesDir}/[...stories].vue` + ], + output: [ + { + name: 'stories-id', + path: '/stories/:id', + file: `${pagesDir}/stories/[id].vue`, + children: [] + }, + { + name: 'stories', + path: '/:stories(.*)*', + file: `${pagesDir}/[...stories].vue`, + children: [] + } + ] + }, { description: 'should generate correct route for snake_case file', files: [