From: Eduardo San Martin Morote Date: Wed, 13 Jul 2022 08:50:36 +0000 (+0200) Subject: fix: allow overriding replace in guards X-Git-Tag: v4.1.3~18 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=f0f448abe0d9c64e35aec8c1906ef4bf7059577f;p=thirdparty%2Fvuejs%2Frouter.git fix: allow overriding replace in guards Fix #1471 --- diff --git a/packages/router/__tests__/router.spec.ts b/packages/router/__tests__/router.spec.ts index bd6b0070..016becc4 100644 --- a/packages/router/__tests__/router.spec.ts +++ b/packages/router/__tests__/router.spec.ts @@ -143,11 +143,31 @@ describe('Router', () => { // move somewhere else await router.push('/search') jest.spyOn(history, 'replace') + jest.spyOn(history, 'push') await router.replace('/home-before') + expect(history.push).toHaveBeenCalledTimes(0) expect(history.replace).toHaveBeenCalledTimes(1) expect(history.replace).toHaveBeenCalledWith('/', expect.anything()) }) + it('replaces if a guard redirect replaces', async () => { + const history = createMemoryHistory() + const { router } = await newRouter({ history }) + // move somewhere else + router.beforeEach(to => { + if (to.name !== 'Foo') { + return { name: 'Foo', replace: true } + } + }) + jest.spyOn(history, 'replace') + jest.spyOn(history, 'push') + await router.push('/search') + expect(history.location).toBe('/foo') + expect(history.push).toHaveBeenCalledTimes(0) + expect(history.replace).toHaveBeenCalledTimes(1) + expect(history.replace).toHaveBeenCalledWith('/foo', expect.anything()) + }) + it('allows to customize parseQuery', async () => { const parseQuery = jest.fn(_ => ({})) const { router } = await newRouter({ parseQuery }) diff --git a/packages/router/src/router.ts b/packages/router/src/router.ts index e44946c2..bd938c0d 100644 --- a/packages/router/src/router.ts +++ b/packages/router/src/router.ts @@ -728,11 +728,17 @@ export function createRouter(options: RouterOptions): Router { return pushWithRedirect( // keep options - assign(locationAsObject(failure.to), { - state: data, - force, - replace, - }), + assign( + { + // preserve an existing replace but allow the redirect to override it + replace, + }, + locationAsObject(failure.to), + { + state: data, + force, + } + ), // preserve the original redirectedFrom if any redirectedFrom || toLocation )