-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New history event proposal #5562
See this exact same proposal but in a different place: WICG/proposals#6
Proposal
Add an event called statechange
or historychange
that will fire on any change to the history stack, whether that be through the browser's back button, or window.history.pushState
or other methods.
This proposed event would be similar to hashchange
fires on all hash changes regardless of the source.
Current Problems
hashchange
events allowed javascript router libraries (e.g. React Router, vue-router) to easily respond to any routing event when the application is using hash routing.
However, with the HTML5 History API, there is no equivalent event that javascript routers can listen to.
This means that routers have the following limitations/problems:
- They assume that they're the only Router that exists on the page - Routers require all code to call into it whenever making a URL change
- Users cannot call
window.history.pushState
directly and must only use the Router's custom methods - Third party libraries that may want to change the URL or cause a Router to update have a difficult time since they can't call native APIs
References:
Example
window.addEventListener('historychange', (event) => {
console.log('changed')
})
history.pushState(null, null, '/path') / logs "changed"
location.hash = "sub" / logs "changed"
Side notes
I hope I did this in the correct repo. Let me know if I need to change/update anything. Thanks.
This was created in collaboration with @joeldenning
Activity