From bed24dff90c19a0ee3e335dbe43ed9ddbfc74542 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Thu, 27 May 2021 11:36:54 +0200 Subject: [PATCH] fix(warn): drop unused params on string redirect Fix #951 --- __tests__/router.spec.ts | 18 ++++++++++++++++++ src/router.ts | 6 +++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/__tests__/router.spec.ts b/__tests__/router.spec.ts index ca4d59800..f55a2798c 100644 --- a/__tests__/router.spec.ts +++ b/__tests__/router.spec.ts @@ -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', @@ -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 }) diff --git a/src/router.ts b/src/router.ts index 8afc901da..833cb210f 100644 --- a/src/router.ts +++ b/src/router.ts @@ -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 (