From: Eduardo San Martin Morote Date: Fri, 17 Dec 2021 07:26:23 +0000 (+0100) Subject: fix: wip X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fheads%2Fdiscussion%2F1219-incorrect-state;p=thirdparty%2Fvuejs%2Frouter.git fix: wip --- diff --git a/__tests__/history/html5.spec.ts b/__tests__/history/html5.spec.ts index 4eb446a3..75cc1375 100644 --- a/__tests__/history/html5.spec.ts +++ b/__tests__/history/html5.spec.ts @@ -1,6 +1,6 @@ import { JSDOM } from 'jsdom' import { createWebHistory } from '../../src/history/html5' -import { createDom } from '../utils' +import { createDom, tick } from '../utils' // override the value of isBrowser because the variable is created before JSDOM // is created @@ -40,6 +40,26 @@ describe('History HTMl5', () => { expect(createWebHistory('#other').base).toBe('#other') }) + it('grecefully handles an invalid state', async () => { + const history = createWebHistory() + const spy = jest.fn() + history.listen(spy) + + // null state + window.history.pushState(null, '') + window.history.back() + await tick(2) + expect(spy).toHaveBeenCalled() + expect(spy).toHaveBeenCalledWith('', '', 'https://example.com/foo') + + // invalid state but not empty + window.history.pushState({}, '') + window.history.back() + await tick() + expect(spy).toHaveBeenCalled() + expect(spy).toHaveBeenCalledWith('', '', 'https://example.com/foo') + }) + it('handles a base tag', () => { const baseEl = document.createElement('base') baseEl.href = '/foo/' diff --git a/src/history/html5.ts b/src/history/html5.ts index 017edef1..7ad31bc4 100644 --- a/src/history/html5.ts +++ b/src/history/html5.ts @@ -69,7 +69,9 @@ function useHistoryListeners( const popStateHandler: PopStateListener = ({ state, }: { - state: StateEntry | null + // use an empty object to accomodate for other kind of invalid states + // https://github.com/vuejs/vue-router-next/pull/1219 + state: StateEntry | null | { current: never } }) => { const to = createCurrentLocation(base, location) const from: HistoryLocation = currentLocation.value @@ -77,6 +79,14 @@ function useHistoryListeners( let delta = 0 if (state) { + if (!state.current) { + state = assign({}, historyState.value, { + back: historyState.value.current, + current: to, + position: historyState.value.position + 1, + }) + } + currentLocation.value = to historyState.value = state