]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
fix(router): stack overflow with redirect
authorEduardo San Martin Morote <posva13@gmail.com>
Tue, 4 Aug 2020 14:22:36 +0000 (16:22 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Tue, 4 Aug 2020 14:22:36 +0000 (16:22 +0200)
Close #404

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

index 98aa0ec94d305560d0704cf5519eb7c86cc1f0ad..b4dbecee3088444294d596e08deb5a20c62d020b 100644 (file)
@@ -217,7 +217,7 @@ describe('Router', () => {
   })
 
   it('navigates if the location does not exist', async () => {
-    const { router } = await newRouter()
+    const { router } = await newRouter({ routes: [routes[0]] })
     const spy = jest.fn((to, from, next) => next())
     router.beforeEach(spy)
     await router.push('/idontexist')
@@ -691,6 +691,26 @@ describe('Router', () => {
         path: '/parent',
       })
     })
+
+    // https://github.com/vuejs/vue-router-next/issues/404
+    it('works with named routes', async () => {
+      const history = createMemoryHistory()
+      const router = createRouter({
+        history,
+        routes: [
+          { name: 'foo', path: '/foo', redirect: '/bar' },
+          { path: '/bar', component: components.Bar },
+        ],
+      })
+      await expect(router.push('/foo')).resolves.toEqual(undefined)
+      const loc = router.currentRoute.value
+      expect(loc.name).toBe(undefined)
+      expect(loc.path).toBe('/bar')
+      expect(loc.redirectedFrom).toMatchObject({
+        name: 'foo',
+        path: '/foo',
+      })
+    })
   })
 
   describe('base', () => {
index 9447208dab50cdf70d1be0e0cd264ba45b4fbbd5..c8a4a58bbd925aec9c91c484547079bd91863144 100644 (file)
@@ -427,13 +427,11 @@ export function createRouter(options: RouterOptions): Router {
       }
       return pushWithRedirect(
         assign(
-          {},
-          // having a path here would be a problem with relative locations but
-          // at the same time it doesn't make sense for a redirect to be
-          // relative (no name, no path) because it would create an infinite
-          // loop. Since newTargetLocation must either have a `path` or a
-          // `name`, this will never happen
-          targetLocation,
+          {
+            query: targetLocation.query,
+            hash: targetLocation.hash,
+            params: targetLocation.params,
+          },
           newTargetLocation,
           {
             state: data,