From: Eduardo San Martin Morote Date: Fri, 11 Sep 2020 10:07:35 +0000 (+0200) Subject: fix(history): gracefully handle empty state X-Git-Tag: v4.0.0-beta.10~33 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cbcf2a95a2af001c8aea96f3c76c4c4ef139219f;p=thirdparty%2Fvuejs%2Frouter.git fix(history): gracefully handle empty state Close #366 --- diff --git a/src/history/html5.ts b/src/history/html5.ts index a093dab3..4d604e75 100644 --- a/src/history/html5.ts +++ b/src/history/html5.ts @@ -243,18 +243,33 @@ function useHistoryStateNavigation(base: string) { function push(to: HistoryLocation, data?: HistoryState) { // Add to current entry the information of where we are going // as well as saving the current position - const currentState: StateEntry = assign({}, history.state, { - forward: to, - scroll: computeScrollPosition(), - }) + const currentState = assign( + {}, + // use current history state to gracefully handle a wrong call to + // history.replaceState + // https://github.com/vuejs/vue-router-next/issues/366 + historyState.value, + history.state as Partial | null, + { + forward: to, + scroll: computeScrollPosition(), + } + ) + + if (__DEV__ && !history.state) { + warn( + `history.state seems to have been manually replaced without preserving the necessary values. Make sure to preserve existing history state if you are manually calling history.replaceState:\n\n` + + `history.replaceState(history.state, '', url)\n\n` + + `You can find more information at https://next.router.vuejs.org/guide/migration/#usage-of-history-state.` + ) + } + changeLocation(currentState.current, currentState, true) const state: StateEntry = assign( {}, buildState(currentLocation.value, to, null), - { - position: currentState.position + 1, - }, + { position: currentState.position + 1 }, data )