From: Eduardo San Martin Morote Date: Tue, 4 Aug 2020 14:22:36 +0000 (+0200) Subject: fix(router): stack overflow with redirect X-Git-Tag: v4.0.0-beta.6~7 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=359401107078348f0410abbd36cffb3b8d4d8f85;p=thirdparty%2Fvuejs%2Frouter.git fix(router): stack overflow with redirect Close #404 --- diff --git a/__tests__/router.spec.ts b/__tests__/router.spec.ts index 98aa0ec9..b4dbecee 100644 --- a/__tests__/router.spec.ts +++ b/__tests__/router.spec.ts @@ -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', () => { diff --git a/src/router.ts b/src/router.ts index 9447208d..c8a4a58b 100644 --- a/src/router.ts +++ b/src/router.ts @@ -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,