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) 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) return prepareRoutes(routes)
} }
const getSortablePath = (path: string) => path.replace(/^\//, '').replace(/:/, 'Z')
function getRoutePath (tokens: SegmentToken[]): string { function getRoutePath (tokens: SegmentToken[]): string {
return tokens.reduce((path, token) => { return tokens.reduce((path, token) => {
return ( return (

View File

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