From 27a0084b6c635adee18c05add41a5281a96876f8 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Thu, 9 Jun 2022 14:03:08 +0100 Subject: [PATCH] fix(nuxt): keep route param optionality when sibling is an index (#5300) --- packages/nuxt/src/pages/utils.ts | 8 -------- packages/nuxt/test/pages.test.ts | 22 ++++++++++++++++++---- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/packages/nuxt/src/pages/utils.ts b/packages/nuxt/src/pages/utils.ts index 2d3670ad68..a39b4b9ae8 100644 --- a/packages/nuxt/src/pages/utils.ts +++ b/packages/nuxt/src/pages/utils.ts @@ -211,14 +211,6 @@ function prepareRoutes (routes: NuxtPage[], parent?: NuxtPage) { route.name = route.name.replace(/-index$/, '') } - if (route.path === '/') { - // Remove ? suffix when index page at same level - routes.forEach((siblingRoute) => { - if (siblingRoute.path.endsWith('?')) { - siblingRoute.path = siblingRoute.path.slice(0, -1) - } - }) - } // Remove leading / if children route if (parent && route.path.startsWith('/')) { route.path = route.path.slice(1) diff --git a/packages/nuxt/test/pages.test.ts b/packages/nuxt/test/pages.test.ts index d5ff507183..a882f74715 100644 --- a/packages/nuxt/test/pages.test.ts +++ b/packages/nuxt/test/pages.test.ts @@ -96,18 +96,32 @@ describe('pages:generateRoutesFromFiles', () => { { description: 'should generate correct dynamic routes', files: [ + `${pagesDir}/index.vue`, `${pagesDir}/[slug].vue`, `${pagesDir}/[[foo]]`, `${pagesDir}/[[foo]]/index.vue`, `${pagesDir}/[bar]/index.vue`, - `${pagesDir}/sub/[slug].vue`, + `${pagesDir}/nonopt/[slug].vue`, + `${pagesDir}/opt/[[slug]].vue`, `${pagesDir}/[[sub]]/route-[slug].vue` ], output: [ { - name: 'sub-slug', - path: '/sub/:slug', - file: `${pagesDir}/sub/[slug].vue`, + name: 'index', + path: '/', + file: `${pagesDir}/index.vue`, + children: [] + }, + { + name: 'nonopt-slug', + path: '/nonopt/:slug', + file: `${pagesDir}/nonopt/[slug].vue`, + children: [] + }, + { + name: 'opt-slug', + path: '/opt/:slug?', + file: `${pagesDir}/opt/[[slug]].vue`, children: [] }, {