fix(nuxt): find parent routes by exact path match (#23040)

This commit is contained in:
Daniel Roe 2023-09-11 11:50:19 +01:00 committed by GitHub
parent 411ecabd10
commit 40601ec15f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 92 additions and 3 deletions

View File

@ -80,7 +80,8 @@ export async function generateRoutesFromFiles (files: string[], pagesDir: string
route.name += (route.name && '/') + segmentName route.name += (route.name && '/') + segmentName
// ex: parent.vue + parent/child.vue // ex: parent.vue + parent/child.vue
const child = parent.find(parentRoute => parentRoute.name === route.name && !parentRoute.path.endsWith('(.*)*')) const path = route.path + getRoutePath(tokens).replace(/\/index$/, '/')
const child = parent.find(parentRoute => parentRoute.name === route.name && parentRoute.path === path)
if (child && child.children) { if (child && child.children) {
parent = child.children parent = child.children

View File

@ -351,6 +351,91 @@ describe('pages:generateRoutesFromFiles', () => {
children: [] children: []
} }
] ]
},
{
description: 'should not merge required param as a child of optional param',
files: [
{ path: `${pagesDir}/[[foo]].vue` },
{ path: `${pagesDir}/[foo].vue` }
],
output: [
{
name: 'foo',
path: '/:foo?',
file: `${pagesDir}/[[foo]].vue`,
children: [
]
},
{
name: 'foo',
path: '/:foo()',
file: `${pagesDir}/[foo].vue`,
children: []
}
]
},
{
description: 'should correctly merge nested routes',
files: [
{ path: `${pagesDir}/param.vue` },
{ path: `${pagesDir}/param/index.vue` },
{ path: `${pagesDir}/param/index/index.vue` },
{ path: `${pagesDir}/param/index/sibling.vue` },
{ path: `${pagesDir}/wrapper-expose/other.vue` },
{ path: `${pagesDir}/wrapper-expose/other/index.vue` },
{ path: `${pagesDir}/wrapper-expose/other/sibling.vue` },
{ path: `${pagesDir}/param/sibling.vue` }
],
output: [
{
children: [
{
children: [
{
children: [],
file: 'pages/param/index/index.vue',
name: 'param-index',
path: ''
},
{
children: [],
file: 'pages/param/index/sibling.vue',
name: 'param-index-sibling',
path: 'sibling'
}
],
file: 'pages/param/index.vue',
path: ''
},
{
children: [],
file: 'pages/param/sibling.vue',
name: 'param-sibling',
path: 'sibling'
}
],
file: 'pages/param.vue',
path: '/param'
},
{
children: [
{
children: [],
file: 'pages/wrapper-expose/other/index.vue',
name: 'wrapper-expose-other',
path: ''
},
{
children: [],
file: 'pages/wrapper-expose/other/sibling.vue',
name: 'wrapper-expose-other-sibling',
path: 'sibling'
}
],
file: 'pages/wrapper-expose/other.vue',
path: '/wrapper-expose/other'
}
]
} }
] ]
@ -360,12 +445,15 @@ describe('pages:generateRoutesFromFiles', () => {
test.files.map(file => [file.path, 'template' in file ? file.template : '']) test.files.map(file => [file.path, 'template' in file ? file.template : ''])
) as Record<string, string> ) as Record<string, string>
let result
try { try {
const result = await generateRoutesFromFiles(test.files.map(file => file.path), pagesDir, true, vfs) result = await generateRoutesFromFiles(test.files.map(file => file.path), pagesDir, true, vfs)
expect(result).to.deep.equal(test.output)
} catch (error: any) { } catch (error: any) {
expect(error.message).toEqual(test.error) expect(error.message).toEqual(test.error)
} }
if (result) {
expect(result).toEqual(test.output)
}
}) })
} }
}) })