fix(nuxt): sort routes with dynamic params at the end (#4983)

This commit is contained in:
Daniel Roe 2022-06-09 13:09:34 +01:00 committed by GitHub
parent 775a904c77
commit b31186b658
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 13 deletions

View File

@ -88,11 +88,15 @@ export function generateRoutesFromFiles (files: string[], pagesDir: string): Nux
}
parent.push(route)
// TODO: https://github.com/vuejs/router/issues/1435
parent.sort((a, b) => getSortablePath(a.path).localeCompare(getSortablePath(b.path)))
}
return prepareRoutes(routes)
}
const getSortablePath = (path: string) => path.replace(/^\//, '').replace(/:/, 'Z')
function getRoutePath (tokens: SegmentToken[]): string {
return tokens.reduce((path, token) => {
return (

View File

@ -105,11 +105,17 @@ describe('pages:generateRoutesFromFiles', () => {
],
output: [
{
name: 'slug',
path: '/:slug',
file: `${pagesDir}/[slug].vue`,
name: 'sub-slug',
path: '/sub/:slug',
file: `${pagesDir}/sub/[slug].vue`,
children: []
},
{
children: [],
name: 'bar',
file: 'pages/[bar]/index.vue',
path: '/:bar'
},
{
children: [
{
@ -124,15 +130,9 @@ describe('pages:generateRoutesFromFiles', () => {
path: '/:foo?'
},
{
children: [],
name: 'bar',
file: 'pages/[bar]/index.vue',
path: '/:bar'
},
{
name: 'sub-slug',
path: '/sub/:slug',
file: `${pagesDir}/sub/[slug].vue`,
name: 'slug',
path: '/:slug',
file: `${pagesDir}/[slug].vue`,
children: []
},
{
@ -145,8 +145,14 @@ describe('pages:generateRoutesFromFiles', () => {
},
{
description: 'should generate correct catch-all route',
files: [`${pagesDir}/[...slug].vue`],
files: [`${pagesDir}/[...slug].vue`, `${pagesDir}/index.vue`],
output: [
{
name: 'index',
path: '/',
file: `${pagesDir}/index.vue`,
children: []
},
{
name: 'slug',
path: '/:slug(.*)*',