]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
fix(history): gracefully handle empty state
authorEduardo San Martin Morote <posva13@gmail.com>
Fri, 11 Sep 2020 10:07:35 +0000 (12:07 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Fri, 11 Sep 2020 10:07:35 +0000 (12:07 +0200)
Close #366

src/history/html5.ts

index a093dab37b85bb6f4579ff6794a389ac59b373db..4d604e7541d3294fb72b0c2cd9a070f8c05e8cab 100644 (file)
@@ -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
     )