Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// we augment the window.history instance so that it will dispatch an event when using push/replace
// this allows us to create event stream and listen to all history state changes
export const patchHistory = history => {
const wrap = m => (state, title, url) => {
m.call(history, state, title, url);
let e = new Event(m.name.toLowerCase());
e.state = state;
dispatchEvent(e);
};
history.pushState = wrap(history.pushState);
history.replaceState = wrap(history.replaceState);
return history;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment