Skip to content

Commit 0fed200

Browse files
committed
ensure wildcard routes are always matched last (fix #1176)
1 parent 16430de commit 0fed200

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/create-route-map.js

+9
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ export function createRouteMap (
2222
addRouteRecord(pathList, pathMap, nameMap, route)
2323
})
2424

25+
// ensure wildcard routes are always at the end
26+
for (let i = 0, l = pathList.length; i < l; i++) {
27+
if (pathList[i] === '*') {
28+
pathList.push(pathList.splice(i, 1)[0])
29+
l--
30+
i--
31+
}
32+
}
33+
2534
return {
2635
pathList,
2736
pathMap,

test/unit/specs/create-map.spec.js

+5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const Baz = { template: '<div>This is Baz</div>' }
99
const routes = [
1010
{ path: '/', name: 'home', component: Home },
1111
{ path: '/foo', name: 'foo', component: Foo },
12+
{ path: '*', name: 'wildcard', component: Baz },
1213
{
1314
path: '/bar',
1415
name: 'bar',
@@ -40,6 +41,10 @@ describe('Creating Route Map', function () {
4041
expect(maps.pathMap['/bar/']).not.toBeUndefined()
4142
})
4243

44+
it('has a pathList which places wildcards at the end', () => {
45+
expect(maps.pathList).toEqual(['', '/foo', '/bar/', '/bar', '*'])
46+
})
47+
4348
it('has a nameMap object for default subroute at \'bar.baz\'', function () {
4449
expect(maps.nameMap['bar.baz']).not.toBeUndefined()
4550
})

0 commit comments

Comments
 (0)