fix(nuxt): keep route param optionality when sibling is an index (#5300)

This commit is contained in:
Daniel Roe 2022-06-09 14:03:08 +01:00 committed by GitHub
parent b31186b658
commit 27a0084b6c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 12 deletions

View File

@ -211,14 +211,6 @@ function prepareRoutes (routes: NuxtPage[], parent?: NuxtPage) {
route.name = route.name.replace(/-index$/, '') 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 // Remove leading / if children route
if (parent && route.path.startsWith('/')) { if (parent && route.path.startsWith('/')) {
route.path = route.path.slice(1) route.path = route.path.slice(1)

View File

@ -96,18 +96,32 @@ describe('pages:generateRoutesFromFiles', () => {
{ {
description: 'should generate correct dynamic routes', description: 'should generate correct dynamic routes',
files: [ files: [
`${pagesDir}/index.vue`,
`${pagesDir}/[slug].vue`, `${pagesDir}/[slug].vue`,
`${pagesDir}/[[foo]]`, `${pagesDir}/[[foo]]`,
`${pagesDir}/[[foo]]/index.vue`, `${pagesDir}/[[foo]]/index.vue`,
`${pagesDir}/[bar]/index.vue`, `${pagesDir}/[bar]/index.vue`,
`${pagesDir}/sub/[slug].vue`, `${pagesDir}/nonopt/[slug].vue`,
`${pagesDir}/opt/[[slug]].vue`,
`${pagesDir}/[[sub]]/route-[slug].vue` `${pagesDir}/[[sub]]/route-[slug].vue`
], ],
output: [ output: [
{ {
name: 'sub-slug', name: 'index',
path: '/sub/:slug', path: '/',
file: `${pagesDir}/sub/[slug].vue`, 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: [] children: []
}, },
{ {