diff --git a/src/history/html5.ts b/src/history/html5.ts index a093dab37..4d604e754 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 )