Skip to content

Commit

Permalink
fix(utils): respect patterns within paths when sorting routes (#20669)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelobotega committed May 22, 2023
1 parent 10787e3 commit 5e9942b
Show file tree
Hide file tree
Showing 4 changed files with 241 additions and 47 deletions.
11 changes: 5 additions & 6 deletions packages/utils/src/route.js
Expand Up @@ -97,7 +97,7 @@ function cleanChildrenRoutes (routes, isChild = false, routeNameSplitter = '-',
return routes
}

const DYNAMIC_ROUTE_REGEX = /^\/([:*])/
const DYNAMIC_ROUTE_REGEX = /[:*]/

export const sortRoutes = function sortRoutes (routes) {
routes.sort((a, b) => {
Expand Down Expand Up @@ -126,13 +126,13 @@ export const sortRoutes = function sortRoutes (routes) {
if (res !== 0) {
break
}
y = _a[i] === '*' ? 2 : _a[i].includes(':') ? 1 : 0
z = _b[i] === '*' ? 2 : _b[i].includes(':') ? 1 : 0
y = _a[i] === '*' ? 3 : _a[i].includes(':') ? 2 : _a[i].includes('*') ? 1 : 0
z = _b[i] === '*' ? 3 : _b[i].includes(':') ? 2 : _b[i].includes('*') ? 1 : 0
res = y - z
// If a.length >= b.length
if (i === _b.length - 1 && res === 0) {
// unless * found sort by level, then alphabetically
res = _a[i] === '*'
res = _a[i].includes('*')
? -1
: (
_a.length === _b.length ? a.path.localeCompare(b.path) : (_a.length - _b.length)
Expand All @@ -142,7 +142,7 @@ export const sortRoutes = function sortRoutes (routes) {

if (res === 0) {
// unless * found sort by level, then alphabetically
res = _a[i - 1] === '*' && _b[i]
res = _a[i - 1].includes('*') && _b[i]
? 1
: (
_a.length === _b.length ? a.path.localeCompare(b.path) : (_a.length - _b.length)
Expand Down Expand Up @@ -248,7 +248,6 @@ const getRoutePathExtension = (key) => {
if (key.startsWith('_')) {
return `:${key.substr(1)}`
}

return key
}

Expand Down
144 changes: 117 additions & 27 deletions packages/utils/test/__snapshots__/route.test.js.snap
Expand Up @@ -26,6 +26,12 @@ exports[`util: route util: route create createRoutes should allow snake case rou
"name": "parent-child-test",
"path": "/parent/child/test",
},
{
"chunkName": "pages/index",
"component": "/some/nuxt/app/pages/index.vue",
"name": "index",
"path": "/",
},
{
"chunkName": "pages/another_route/_id",
"component": "/some/nuxt/app/pages/another_route/_id.vue",
Expand All @@ -44,12 +50,6 @@ exports[`util: route util: route create createRoutes should allow snake case rou
"name": "parent-all",
"path": "/parent/*",
},
{
"chunkName": "pages/index",
"component": "/some/nuxt/app/pages/index.vue",
"name": "index",
"path": "/",
},
{
"chunkName": "pages/_param",
"component": "/some/nuxt/app/pages/_param.vue",
Expand Down Expand Up @@ -85,6 +85,12 @@ exports[`util: route util: route create createRoutes should allow snake case rou
"name": "parent-child-test",
"path": "/parent/child/test",
},
{
"chunkName": "pages/index",
"component": "\\\\\\\\some\\\\nuxt\\\\app\\\\pages\\\\index.vue",
"name": "index",
"path": "/",
},
{
"chunkName": "pages/another_route/_id",
"component": "\\\\\\\\some\\\\nuxt\\\\app\\\\pages\\\\another_route\\\\_id.vue",
Expand All @@ -103,12 +109,6 @@ exports[`util: route util: route create createRoutes should allow snake case rou
"name": "parent-all",
"path": "/parent/*",
},
{
"chunkName": "pages/index",
"component": "\\\\\\\\some\\\\nuxt\\\\app\\\\pages\\\\index.vue",
"name": "index",
"path": "/",
},
{
"chunkName": "pages/_param",
"component": "\\\\\\\\some\\\\nuxt\\\\app\\\\pages\\\\_param.vue",
Expand Down Expand Up @@ -156,6 +156,15 @@ exports[`util: route util: route create createRoutes should enforce trailing sla
"strict": true,
},
},
{
"chunkName": "pages/index",
"component": "/some/nuxt/app/pages/index.vue",
"name": "index",
"path": "/",
"pathToRegexpOptions": {
"strict": true,
},
},
{
"chunkName": "pages/another_route/_id",
"component": "/some/nuxt/app/pages/another_route/_id.vue",
Expand Down Expand Up @@ -183,15 +192,6 @@ exports[`util: route util: route create createRoutes should enforce trailing sla
"strict": true,
},
},
{
"chunkName": "pages/index",
"component": "/some/nuxt/app/pages/index.vue",
"name": "index",
"path": "/",
"pathToRegexpOptions": {
"strict": true,
},
},
{
"chunkName": "pages/_param",
"component": "/some/nuxt/app/pages/_param.vue",
Expand Down Expand Up @@ -242,6 +242,15 @@ exports[`util: route util: route create createRoutes should remove trailing slas
"strict": true,
},
},
{
"chunkName": "pages/index",
"component": "/some/nuxt/app/pages/index.vue",
"name": "index",
"path": "/",
"pathToRegexpOptions": {
"strict": true,
},
},
{
"chunkName": "pages/another_route/_id",
"component": "/some/nuxt/app/pages/another_route/_id.vue",
Expand Down Expand Up @@ -269,23 +278,104 @@ exports[`util: route util: route create createRoutes should remove trailing slas
"strict": true,
},
},
{
"chunkName": "pages/_param",
"component": "/some/nuxt/app/pages/_param.vue",
"name": "param",
"path": "/:param",
"pathToRegexpOptions": {
"strict": true,
},
},
]
`;

exports[`util: route util: route sortRoutes sortRoutes should sort routes 1`] = `
[
{
"chunkName": "pages/de/index",
"component": "/some/nuxt/app/pages/de/index.vue",
"name": "de",
"path": "/de",
},
{
"chunkName": "pages/de_",
"component": "/some/nuxt/app/pages/de_.vue",
"name": "de_",
"path": "/de_",
},
{
"chunkName": "pages/parent/index",
"component": "/some/nuxt/app/pages/parent/index.vue",
"name": "parent",
"path": "/parent",
},
{
"chunkName": "pages/snake_case_route",
"component": "/some/nuxt/app/pages/snake_case_route.vue",
"name": "snake_case_route",
"path": "/snake_case_route",
},
{
"children": [
{
"chunkName": "pages/another_route/rout_",
"component": "/some/nuxt/app/pages/another_route/rout_.vue",
"name": "another_route-rout_",
"path": "",
},
],
"chunkName": "pages/another_route/rout_",
"component": "/some/nuxt/app/pages/another_route/rout_.vue",
"path": "/another_route/rout_",
},
{
"chunkName": "pages/parent/child/index",
"component": "/some/nuxt/app/pages/parent/child/index.vue",
"name": "parent-child",
"path": "/parent/child",
},
{
"chunkName": "pages/parent/child/test",
"component": "/some/nuxt/app/pages/parent/child/test.vue",
"name": "parent-child-test",
"path": "/parent/child/test",
},
{
"chunkName": "pages/index",
"component": "/some/nuxt/app/pages/index.vue",
"name": "index",
"path": "/",
"pathToRegexpOptions": {
"strict": true,
},
},
{
"chunkName": "pages/another_route/_id",
"component": "/some/nuxt/app/pages/another_route/_id.vue",
"name": "another_route-id",
"path": "/another_route/:id?",
},
{
"chunkName": "pages/subpage/_param",
"component": "/some/nuxt/app/pages/subpage/_param.vue",
"name": "subpage-param",
"path": "/subpage/:param?",
},
{
"chunkName": "pages/parent/_",
"component": "/some/nuxt/app/pages/parent/_.vue",
"name": "parent-all",
"path": "/parent/*",
},
{
"chunkName": "pages/_param",
"component": "/some/nuxt/app/pages/_param.vue",
"name": "param",
"path": "/:param",
"pathToRegexpOptions": {
"strict": true,
},
},
{
"chunkName": "pages/_",
"component": "/some/nuxt/app/pages/_.vue",
"name": "all",
"path": "/*",
},
]
`;
107 changes: 106 additions & 1 deletion packages/utils/test/route.test.js
@@ -1,4 +1,4 @@
import { flatRoutes, createRoutes, guardDir, promisifyRoute } from '../src/route'
import { flatRoutes, createRoutes, guardDir, promisifyRoute, sortRoutes } from '../src/route'

describe('util: route', () => {
test('should flat route with path', () => {
Expand Down Expand Up @@ -238,4 +238,109 @@ describe('util: route', () => {
expect(routesResult).toMatchSnapshot()
})
})

describe('util: route sortRoutes', () => {
const files = [
'pages/_param.vue',
'pages/de/index.vue',
'pages/index.vue',
'pages/_.vue',
'pages/another_route/rout_.vue',
'pages/snake_case_route.vue',
'pages/subpage/_param.vue',
'pages/de_.vue',
'pages/parent/index.vue',
'pages/parent/_.vue',
'pages/another_route/_id.vue',
'pages/another_route/rout_.vue',
'pages/parent/child/index.vue',
'pages/parent/child/test.vue'
]
const srcDir = '/some/nuxt/app'
const pagesDir = 'pages'

test.posix('sortRoutes should sort routes', () => {
const routesResult = createRoutes({ files, srcDir, pagesDir })
expect(routesResult).toMatchSnapshot()
})
test('Should sortRoutes with extendRoutes using *', () => {
const routes = [
{ path: '/poetry' },
{ path: '/reports' },
{ path: '*' },
{ path: '/de/about' },
{ path: '/' },
{ path: '/about' },
{ path: '/de' },
{ path: '/tech' },
{ path: '/de/tech' },
{ path: '/de*' },
{ path: '/:post' },
{ path: '/de/:post' },
{ path: '/de/reports' },
{ path: '/de/poetry' }
]

sortRoutes(routes)

expect(routes).toEqual(
[
{ path: '/about' },
{ path: '/de' },
{ path: '/poetry' },
{ path: '/reports' },
{ path: '/tech' },
{ path: '/de/about' },
{ path: '/de/poetry' },
{ path: '/de/reports' },
{ path: '/de/tech' },
{ path: '/' },
{ path: '/de/:post' },
{ path: '/de*' },
{ path: '/:post' },
{ path: '*' }
]
)
})

test('Should sortRoutes with extendRoutes using /*', () => {
const routes = [
{ path: '/poetry' },
{ path: '/reports' },
{ path: '/*' },
{ path: '/de/about' },
{ path: '/about' },
{ path: '/de' },
{ path: '/tech' },
{ path: '/de/tech' },
{ path: '/de/*' },
{ path: '/' },
{ path: '/:post' },
{ path: '/de/:post' },
{ path: '/de/reports' },
{ path: '/de/poetry' }
]

sortRoutes(routes)

expect(routes).toEqual(
[
{ path: '/about' },
{ path: '/de' },
{ path: '/poetry' },
{ path: '/reports' },
{ path: '/tech' },
{ path: '/de/about' },
{ path: '/de/poetry' },
{ path: '/de/reports' },
{ path: '/de/tech' },
{ path: '/' },
{ path: '/de/:post' },
{ path: '/de/*' },
{ path: '/:post' },
{ path: '/*' }
]
)
})
})
})

0 comments on commit 5e9942b

Please sign in to comment.