fix(nuxt): handle optional params within a path segment (#23070)

This commit is contained in:
Dmitry Istomin 2023-09-11 11:13:24 +03:00 committed by GitHub
parent a07b34a6ed
commit 48fa30af92
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 1 deletions

View File

@ -220,7 +220,7 @@ function parseSegment (segment: string) {
if (c === '[' && state === SegmentParserState.dynamic) { if (c === '[' && state === SegmentParserState.dynamic) {
state = SegmentParserState.optional state = SegmentParserState.optional
} }
if (c === ']' && (state !== SegmentParserState.optional || buffer[buffer.length - 1] === ']')) { if (c === ']' && (state !== SegmentParserState.optional || segment[i - 1] === ']')) {
if (!buffer) { if (!buffer) {
throw new Error('Empty param') throw new Error('Empty param')
} else { } else {

View File

@ -157,6 +157,10 @@ describe('pages:generateRoutesFromFiles', () => {
{ path: `${pagesDir}/[slug].vue` }, { path: `${pagesDir}/[slug].vue` },
{ path: `${pagesDir}/[[foo]]` }, { path: `${pagesDir}/[[foo]]` },
{ path: `${pagesDir}/[[foo]]/index.vue` }, { 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}/[bar]/index.vue` },
{ path: `${pagesDir}/nonopt/[slug].vue` }, { path: `${pagesDir}/nonopt/[slug].vue` },
{ path: `${pagesDir}/opt/[[slug]].vue` }, { path: `${pagesDir}/opt/[[slug]].vue` },
@ -188,6 +192,31 @@ describe('pages:generateRoutesFromFiles', () => {
file: 'pages/[[foo]]', file: 'pages/[[foo]]',
path: '/: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: [], children: [],
name: 'bar', name: 'bar',