Skip to content

Commit

Permalink
fix(warn): drop unused params on string redirect
Browse files Browse the repository at this point in the history
Fix #951
  • Loading branch information
posva committed May 27, 2021
1 parent 69709f5 commit bed24df
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
18 changes: 18 additions & 0 deletions __tests__/router.spec.ts
Expand Up @@ -36,6 +36,7 @@ const routes: RouteRecordRaw[] = [
{ path: '/p/:p', name: 'Param', component: components.Bar },
{ path: '/repeat/:r+', name: 'repeat', component: components.Bar },
{ path: '/to-p/:p', redirect: to => `/p/${to.params.p}` },
{ path: '/redirect-with-param/:p', redirect: () => `/` },
{ path: '/before-leave', component: components.BeforeLeave },
{
path: '/parent',
Expand Down Expand Up @@ -624,6 +625,23 @@ describe('Router', () => {
})
})

it('discard params on string redirect', async () => {
const history = createMemoryHistory()
const router = createRouter({ history, routes })
await expect(router.push('/redirect-with-param/test')).resolves.toEqual(
undefined
)
expect(router.currentRoute.value).toMatchObject({
params: {},
query: {},
hash: '',
redirectedFrom: expect.objectContaining({
fullPath: '/redirect-with-param/test',
params: { p: 'test' },
}),
})
})

it('allows object in redirect', async () => {
const history = createMemoryHistory()
const router = createRouter({ history, routes })
Expand Down
6 changes: 5 additions & 1 deletion src/router.ts
Expand Up @@ -577,7 +577,11 @@ export function createRouter(options: RouterOptions): Router {
newTargetLocation.indexOf('?') > -1 ||
newTargetLocation.indexOf('#') > -1
? (newTargetLocation = locationAsObject(newTargetLocation))
: { path: newTargetLocation }
: // force empty params
{ path: newTargetLocation }
// @ts-expect-error: force empty params when a string is passed to let
// the router parse them again
newTargetLocation.params = {}
}

if (
Expand Down

0 comments on commit bed24df

Please sign in to comment.