Skip to content

Commit

Permalink
fix(matcher): addRoutes navigate to same as current location (#3228)
Browse files Browse the repository at this point in the history
Checking for the length is not enough when having a path: '*' route
Closes #3216
  • Loading branch information
posva authored Jun 12, 2020
2 parents 9ff90bd + 62598b9 commit 0eaae3a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/history/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,13 @@ export class History {
}
onAbort && onAbort(err)
}
const lastRouteIndex = route.matched.length - 1
const lastCurrentIndex = current.matched.length - 1
if (
isSameRoute(route, current) &&
// in the case the route map has been dynamically appended to
route.matched.length === current.matched.length
lastRouteIndex === lastCurrentIndex &&
route.matched[lastRouteIndex] === current.matched[lastCurrentIndex]
) {
this.ensureURL()
return abort(createNavigationDuplicatedError(current, route))
Expand Down
17 changes: 17 additions & 0 deletions test/unit/specs/api.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,23 @@ describe('router.addRoutes', () => {
expect(components.length).toBe(1)
expect(components[0].name).toBe('A')
})

it('allows navigating to the same as current location', () => {
const router = new Router({
routes: [
{ path: '/', component: {}},
{ path: '*', component: {}}
]
})

router.push('/not-found')

expect(router.currentRoute.params).toEqual({ pathMatch: '/not-found' })
router.addRoutes([{ path: '/not-found', component: {}}])

// the navigation should have changed locations
expect(router.currentRoute.params).toEqual({})
})
})

describe('router.push/replace', () => {
Expand Down

0 comments on commit 0eaae3a

Please sign in to comment.