From: Eduardo San Martin Morote Date: Mon, 16 May 2022 13:24:19 +0000 (+0200) Subject: fix: discard params in path redirect X-Git-Tag: v4.1.0~93 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8d12db4ffd2406dcc45777a68c6e601546dfce36;p=thirdparty%2Fvuejs%2Frouter.git fix: discard params in path redirect Fix #1401 --- diff --git a/__tests__/warnings.spec.ts b/__tests__/warnings.spec.ts index e749472b..b6ac7608 100644 --- a/__tests__/warnings.spec.ts +++ b/__tests__/warnings.spec.ts @@ -32,7 +32,7 @@ describe('warnings', () => { history, routes: [{ path: '/:p', name: 'p', component }], }) - router.resolve({ path: '/p', params: { p: 'p' } }) + router.push({ path: '/p', params: { p: 'p' } }) expect('Path "/p" was passed with params').toHaveBeenWarned() }) @@ -42,10 +42,26 @@ describe('warnings', () => { history, routes: [{ path: '/:p', name: 'p', component }], }) - router.resolve({ path: '/p', name: 'p', params: { p: 'p' } }) + router.push({ path: '/p', name: 'p', params: { p: 'p' } }) expect('Path "/" was passed with params').not.toHaveBeenWarned() }) + it('does not warn when redirecting from params', async () => { + const history = createMemoryHistory() + const router = createRouter({ + history, + routes: [ + { + path: '/p/:p', + redirect: to => ({ path: '/s', query: { p: to.params.p } }), + }, + { path: '/s', component }, + ], + }) + router.push({ path: '/p/abc' }) + expect('was passed with params').not.toHaveBeenWarned() + }) + it('warns if an alias is missing params', async () => { createRouter({ history: createMemoryHistory(), diff --git a/src/router.ts b/src/router.ts index 7f0f9d1f..abd238b7 100644 --- a/src/router.ts +++ b/src/router.ts @@ -650,7 +650,8 @@ export function createRouter( { query: to.query, hash: to.hash, - params: to.params, + // avoid transferring params if the redirect has a path + params: 'path' in newTargetLocation ? {} : to.params, }, newTargetLocation )