]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
fix(warn): drop unused params on string redirect
authorEduardo San Martin Morote <posva13@gmail.com>
Thu, 27 May 2021 09:36:54 +0000 (11:36 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Thu, 27 May 2021 09:36:54 +0000 (11:36 +0200)
Fix #951

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

index ca4d59800fb28c20e9cc6a8841e3b2a08613c942..f55a2798c0de32ab677bd5a1b30f2d5c8090a734 100644 (file)
@@ -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 })
index 8afc901dabff3aef2d85a1e5e26a590414419bf0..833cb210fab8c299ca9cbd4aba9ecc06dfa5e314 100644 (file)
@@ -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 (