Skip to content

Commit

Permalink
fix(history): gracefully handle empty state
Browse files Browse the repository at this point in the history
Close #366
  • Loading branch information
posva committed Sep 11, 2020
1 parent 70ce7fe commit cbcf2a9
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions src/history/html5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<StateEntry> | 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
)

Expand Down

0 comments on commit cbcf2a9

Please sign in to comment.