You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Aug 21, 2026. It is now read-only.
Repository navigation
This repository was archived by the owner on Aug 21, 2026. It is now read-only.
When should app history state be carried over? #100
The following carries over app history state to the next entry:
Fragment navigation, including appHistory.navigate("#foo") and location.hash = "foo"
The following reload the current entry and so leave the app history state as-is:
appHistory.navigate({ navigateInfo })
location.reload()
The following replace current entry with a new entry whose the app history state is undefined:
history.replaceState()
appHistory.navigate(appHistory.current.url)
appHistory.navigate(appHistory.current.url, { replace: true }) (here replace is redundant)
appHistory.navigate(otherURL, { replace: true })
location.href = location.href
(Note: in general same-URL navigations are not interoperable for history.state: whatwg/html#6213 . We need to pick a behavior and test it to death for app history. Currently we've picked reset to undefined.)
The following cases push a new entry and do not carry over the app history state, so the new state will be undefined:
history.pushState()
appHistory.navigate(otherURL)
location.href = otherURL
The following, of course, explicitly set the app history state to the new value:
This doesn't seem very principled. I think what happened here is that we heard "it's annoying that history.state gets reset on fragment navigations", and fixed that. But then we kept doing the natural-from-the-spec's-point-of-view thing and reset by default.
I think I have two main questions:
Should replaces carry over the state by default? (I.e., always carry over, unless you do appHistory.navigate(url, { state, replace: true }) with a new state value.)
Should we carry over the state on any of the same-document push cases? Recall that appHistory.navigate(otherURL) and location.href = otherURL can all be turned into same-document navigations via the navigate event.
We experimented previously with carrying over state by default in all cases. It seemed like a good idea initially but I regretted it pretty quickly.
The trouble was that most state which made sense to carry forward automatically turned out not to really belong to the state of any history entry at all. Although I suspect there really are cases where it would make sense, the automatic carrying-forward encouraged usage of history state as if it were interchangeable with sessionStorage, and because folks navigate forwards a lot more often than backwards, it was easy to miss when history entry state was being used to model/persist application state for which “subset of history entries” was not really the intended “key”.
We’re still doing automatic carry over on replacement, but I plan to get rid of this too; the problem seems no different there. The exception is the hashchange case, where carryover is definitely helpful and has helped fix problems (rather than creating new ones).
Should add that despite having gotten rid of automatic carryover for pushes, I still don’t feel confident that where we landed is a great API. We’re doing the same thing as here & in History: “state” is just a blob getting structured-cloned. The trouble that remains is that it’s tricky to ensure some specific facet of state (it’s not nec “everything in state”) does get carried forward in the cases where that really is the intention. I don’t know if addressing that falls within the scope of AppHistory, though, and I haven’t had any good ideas for how to solve it myself yet.
That's extremely helpful insight, @bathos. It's good to hear that maybe where we've currently landed---only carryover on fragment navigations---is not so terrible after all.
Activity
bathos commentedon Apr 22, 2021
We experimented previously with carrying over state by default in all cases. It seemed like a good idea initially but I regretted it pretty quickly.
The trouble was that most state which made sense to carry forward automatically turned out not to really belong to the state of any history entry at all. Although I suspect there really are cases where it would make sense, the automatic carrying-forward encouraged usage of history state as if it were interchangeable with sessionStorage, and because folks navigate forwards a lot more often than backwards, it was easy to miss when history entry state was being used to model/persist application state for which “subset of history entries” was not really the intended “key”.
We’re still doing automatic carry over on replacement, but I plan to get rid of this too; the problem seems no different there. The exception is the hashchange case, where carryover is definitely helpful and has helped fix problems (rather than creating new ones).
Should add that despite having gotten rid of automatic carryover for pushes, I still don’t feel confident that where we landed is a great API. We’re doing the same thing as here & in History: “state” is just a blob getting structured-cloned. The trouble that remains is that it’s tricky to ensure some specific facet of state (it’s not nec “everything in state”) does get carried forward in the cases where that really is the intention. I don’t know if addressing that falls within the scope of AppHistory, though, and I haven’t had any good ideas for how to solve it myself yet.
domenic commentedon Apr 22, 2021
That's extremely helpful insight, @bathos. It's good to hear that maybe where we've currently landed---only carryover on fragment navigations---is not so terrible after all.