I’m calling history.back()
and history.forward()
, but I want to have a popstate
handler just for events triggered by the browser. In other words, I’d like to ignore popstate
s triggered programmatically. Is this possible?
The only possible solutions I can think of involve keeping track of all the history states in my webapp, then every time I call history.back()
, I’d know what state would be in event.state
, so I know what to ignore. However, I can think of edge cases where this might not work: if a lot of navigations happen before popstate
runs, which is unlikely but possible.
Is there a better way?
Context:
Currently, my webapp updates app state on popstate
, but this is noticeably laggy on slower devices. E.g. user clicks an in-app “back” button -> trigger history.back()
-> popstate
handler -> update app state -> rerender. Instead, I want to change it to: user clicks an in-app “back” button -> update app state -> rerender -> trigger history.back()
. I still need the popstate
handler for browser backs. If the user navigates multiple times very quickly, then the popstate
handler would behave weirdly, which I want to want to avoid.