From 48fa30af925289c86fc45e2943f2d2027390f7a4 Mon Sep 17 00:00:00 2001 From: Dmitry Istomin Date: Mon, 11 Sep 2023 11:13:24 +0300 Subject: [PATCH] fix(nuxt): handle optional params within a path segment (#23070) --- packages/nuxt/src/pages/utils.ts | 2 +- packages/nuxt/test/pages.test.ts | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/packages/nuxt/src/pages/utils.ts b/packages/nuxt/src/pages/utils.ts index 7e20e617b3..e095bd3622 100644 --- a/packages/nuxt/src/pages/utils.ts +++ b/packages/nuxt/src/pages/utils.ts @@ -220,7 +220,7 @@ function parseSegment (segment: string) { if (c === '[' && state === SegmentParserState.dynamic) { state = SegmentParserState.optional } - if (c === ']' && (state !== SegmentParserState.optional || buffer[buffer.length - 1] === ']')) { + if (c === ']' && (state !== SegmentParserState.optional || segment[i - 1] === ']')) { if (!buffer) { throw new Error('Empty param') } else { diff --git a/packages/nuxt/test/pages.test.ts b/packages/nuxt/test/pages.test.ts index 8d21d91264..1de27d84f9 100644 --- a/packages/nuxt/test/pages.test.ts +++ b/packages/nuxt/test/pages.test.ts @@ -157,6 +157,10 @@ describe('pages:generateRoutesFromFiles', () => { { path: `${pagesDir}/[slug].vue` }, { path: `${pagesDir}/[[foo]]` }, { path: `${pagesDir}/[[foo]]/index.vue` }, + { path: `${pagesDir}/optional/[[opt]].vue` }, + { path: `${pagesDir}/optional/prefix-[[opt]].vue` }, + { path: `${pagesDir}/optional/[[opt]]-postfix.vue` }, + { path: `${pagesDir}/optional/prefix-[[opt]]-postfix.vue` }, { path: `${pagesDir}/[bar]/index.vue` }, { path: `${pagesDir}/nonopt/[slug].vue` }, { path: `${pagesDir}/opt/[[slug]].vue` }, @@ -188,6 +192,31 @@ describe('pages:generateRoutesFromFiles', () => { file: 'pages/[[foo]]', path: '/:foo?' }, + { + children: [], + path: '/optional/:opt?', + name: 'optional-opt', + file: `${pagesDir}/optional/[[opt]].vue` + }, + { + children: [], + path: '/optional/prefix-:opt?', + name: 'optional-prefix-opt', + file: `${pagesDir}/optional/prefix-[[opt]].vue` + }, + + { + children: [], + path: '/optional/:opt?-postfix', + name: 'optional-opt-postfix', + file: `${pagesDir}/optional/[[opt]]-postfix.vue` + }, + { + children: [], + path: '/optional/prefix-:opt?-postfix', + name: 'optional-prefix-opt-postfix', + file: `${pagesDir}/optional/prefix-[[opt]]-postfix.vue` + }, { children: [], name: 'bar',