Skip to content
Source
Sign inSign up
TanStack/router

fix(history): keep flushing after a native history call throws #8787

Opentuanphungcz(GitHub profile, opens in a new tab)wants to merge 3 commits intomain fromrefs/pull/8787/headopened

Showing 3 changed files with 112 additions and 13 deletions.

x
0 of 3 viewed, 3 left
5 additions and 0 deletions.changeset/history-flush-after-throw.mdAdded
View file
@@ -0,0 +1,5 @@
+---
+'@tanstack/history': patch
+---
+​
+Keep updating the URL after a native `pushState` / `replaceState` call throws once in browser history. Previously the queued action was never cleared, so no later navigation reached the address bar until a reload. The failed update now also restores the history location from the browser, so the next push gets the right index and back navigation is reported as `BACK`.
25 additions and 13 deletionspackages/history/src/index.tsModified
View file
@@ -370,22 +370,34 @@
return
​
- // We need to ignore any updates to the subscribers while we update the browser history
- history._ignoreSubscribers = true
+ const [href, state, isPush] = next
​
- // Update the browser history
- ;(next[2 /* is push */] ? win.history.pushState : win.history.replaceState)(
- next[1 /* state */],
- '',
- next[0 /* href */],
- )
-​
- // Stop ignoring subscriber updates
- history._ignoreSubscribers = false
-​
- // Clear the queued action after it reaches browser history.
+ // Clear the queued action before the native call: if it throws (e.g. a
+ // third-party wrapper or the browser refusing the update), later actions
+ // must still be able to schedule their own flush.