Skip to content

Instantly share code, notes, and snippets.

@Ivannnnn
Created October 10, 2019 09:01
Show Gist options
  • Save Ivannnnn/d66dab04fdf25e49fb792cf93a399199 to your computer and use it in GitHub Desktop.
Save Ivannnnn/d66dab04fdf25e49fb792cf93a399199 to your computer and use it in GitHub Desktop.
Subscribe to url change
const onLocationChange = (() => {
const subscribers = []
const fireEvent = () => subscribers.forEach(sub => sub())
const { pushState, replaceState } = window.history
window.history.pushState = function(...args) {
pushState.apply(window.history, args)
fireEvent()
}
window.history.replaceState = function(...args) {
replaceState.apply(window.history, args)
fireEvent()
}
window.addEventListener('popstate', fireEvent)
return subscriber => subscribers.push(subscriber)
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment