Skip to content

Commit

Permalink
ensure wildcard routes are always matched last (fix #1176)
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Apr 14, 2017
1 parent 16430de commit 0fed200
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/create-route-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ export function createRouteMap (
addRouteRecord(pathList, pathMap, nameMap, route)
})

// ensure wildcard routes are always at the end
for (let i = 0, l = pathList.length; i < l; i++) {
if (pathList[i] === '*') {
pathList.push(pathList.splice(i, 1)[0])
l--
i--
}
}

return {
pathList,
pathMap,
Expand Down
5 changes: 5 additions & 0 deletions test/unit/specs/create-map.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const Baz = { template: '<div>This is Baz</div>' }
const routes = [
{ path: '/', name: 'home', component: Home },
{ path: '/foo', name: 'foo', component: Foo },
{ path: '*', name: 'wildcard', component: Baz },
{
path: '/bar',
name: 'bar',
Expand Down Expand Up @@ -40,6 +41,10 @@ describe('Creating Route Map', function () {
expect(maps.pathMap['/bar/']).not.toBeUndefined()
})

it('has a pathList which places wildcards at the end', () => {
expect(maps.pathList).toEqual(['', '/foo', '/bar/', '/bar', '*'])
})

it('has a nameMap object for default subroute at \'bar.baz\'', function () {
expect(maps.nameMap['bar.baz']).not.toBeUndefined()
})
Expand Down

0 comments on commit 0fed200

Please sign in to comment.