]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
fix(router): make redirect relative to target location
authorEduardo San Martin Morote <posva13@gmail.com>
Wed, 22 Apr 2020 21:02:13 +0000 (23:02 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Wed, 22 Apr 2020 21:02:13 +0000 (23:02 +0200)
__tests__/router.spec.ts
src/router.ts

index d39568083c158e632d738c3207c2249a09dcaa10..d158937d4bcec02270884b1320f31ac0a8a17bac 100644 (file)
@@ -26,6 +26,7 @@ const routes: RouteRecordRaw[] = [
   { path: '/to-foo', redirect: '/foo' },
   { path: '/to-foo-named', redirect: { name: 'Foo' } },
   { path: '/to-foo2', redirect: '/to-foo' },
+  { path: '/to-p/:p', redirect: { name: 'Param' } },
   { 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}` },
@@ -523,18 +524,38 @@ describe('Router', () => {
       })
     })
 
-    it('drops query and params on redirect if not provided', async () => {
+    it('keeps query and hash when redirect is a string', async () => {
       const history = createMemoryHistory()
       const router = createRouter({ history, routes })
       await expect(router.push('/to-foo?hey=foo#fa')).resolves.toEqual(
         undefined
       )
-      const loc = router.currentRoute.value
-      expect(loc.name).toBe('Foo')
-      expect(loc.query).toEqual({})
-      expect(loc.hash).toBe('')
-      expect(loc.redirectedFrom).toMatchObject({
-        path: '/to-foo',
+      expect(router.currentRoute.value).toMatchObject({
+        name: 'Foo',
+        path: '/foo',
+        params: {},
+        query: { hey: 'foo' },
+        hash: '#fa',
+        redirectedFrom: expect.objectContaining({
+          fullPath: '/to-foo?hey=foo#fa',
+        }),
+      })
+    })
+
+    it('keeps params, query and hash from targetLocation on redirect', async () => {
+      const history = createMemoryHistory()
+      const router = createRouter({ history, routes })
+      await expect(router.push('/to-p/1?hey=foo#fa')).resolves.toEqual(
+        undefined
+      )
+      expect(router.currentRoute.value).toMatchObject({
+        name: 'Param',
+        params: { p: '1' },
+        query: { hey: 'foo' },
+        hash: '#fa',
+        redirectedFrom: expect.objectContaining({
+          fullPath: '/to-p/1?hey=foo#fa',
+        }),
       })
     })
 
index cb10805b75da2cce2ba12eaf362b8afb745081c7..48870c7f9c2d7dbe082cad7f4da428e275ebc772 100644 (file)
@@ -314,7 +314,13 @@ export function createRouter({
         typeof redirect === 'function' ? redirect(targetLocation) : redirect
       )
       return pushWithRedirect(
-        { ...newTargetLocation, state: data, force, replace },
+        {
+          ...targetLocation,
+          ...newTargetLocation,
+          state: data,
+          force,
+          replace,
+        },
         // keep original redirectedFrom if it exists
         redirectedFrom || targetLocation
       )