]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
fix: discard params in path redirect
authorEduardo San Martin Morote <posva13@gmail.com>
Mon, 16 May 2022 13:24:19 +0000 (15:24 +0200)
committerEduardo San Martin Morote <posva@users.noreply.github.com>
Thu, 30 Jun 2022 07:59:00 +0000 (09:59 +0200)
Fix #1401

__tests__/warnings.spec.ts
src/router.ts

index e749472b741ea52171fa675600980caad4382dbb..b6ac76087e184ea3e50c687172f38375b166ecb5 100644 (file)
@@ -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(),
index 7f0f9d1f958eba0a4ac3668ef69a28247d2d1240..abd238b7a25067ddd398454eb73dfaaf94a49287 100644 (file)
@@ -650,7 +650,8 @@ export function createRouter<Options extends RouterOptions>(
         {
           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
       )