Handle wildcard routing order

This commit is contained in:
Sébastien Chopin 2017-08-14 14:01:10 +02:00
parent 700577a526
commit db47df0761
9 changed files with 32 additions and 9 deletions

View File

@ -219,21 +219,26 @@ export function createRoutes (files, srcDir) {
if (!b.path.length || b.path === '/') {
return 1
}
let i = 0
let res = 0
let _a = a.path.split('/')
let _b = b.path.split('/')
for (let i = 0; i < _a.length; i++) {
let y = 0
let z = 0
const _a = a.path.split('/')
const _b = b.path.split('/')
for (i = 0; i < _a.length; i++) {
if (res !== 0) {
break
}
let y = (_a[i].indexOf('*') > -1) ? 2 : (_a[i].indexOf(':') > -1 ? 1 : 0)
let z = (_b[i].indexOf('*') > -1) ? 2 : (_b[i].indexOf(':') > -1 ? 1 : 0)
y = _a[i] === '*' ? 2 : (_a[i].indexOf(':') > -1 ? 1 : 0)
z = _b[i] === '*' ? 2 : (_b[i].indexOf(':') > -1 ? 1 : 0)
res = y - z
// If a.length >= b.length
if (i === _b.length - 1 && res === 0) {
res = 1
// change order if * found
res = _a[i] === '*' ? -1 : 1
}
}
return res === 0 ? -1 : res
return res === 0 ? (_a[i - 1] === '*' && _b[i] ? 1 : -1) : res
})
})
return cleanChildrenRoutes(routes)

View File

@ -75,7 +75,13 @@ test('Check .nuxt/router.js', t => {
t.is(routes[12].path, '/:key/:id?')
t.is(routes[12].name, 'key-id')
// pages/_.vue
t.is(routes[13].path, '/*')
t.is(routes[13].name, 'all')
t.is(routes[13].path, '/*/p/*')
t.is(routes[13].name, 'all-p-all')
// pages/_/_.vue
t.is(routes[14].path, '/*/*')
t.is(routes[14].name, 'all-all')
// pages/_.vue
t.is(routes[15].path, '/*')
t.is(routes[15].name, 'all')
})
})

View File

View File

View File

@ -0,0 +1,3 @@
<template>
<h1>pages/_key/_id.vue</h1>
</template>

View File

@ -0,0 +1,3 @@
<template>
<h1>pages/_slug.vue</h1>
</template>

View File

@ -0,0 +1,3 @@
<template>
<h1>pages/test/_.vue</h1>
</template>

View File

@ -0,0 +1,3 @@
<template>
<h1>pages/test/index.vue</h1>
</template>