Skip to content

Commit

Permalink
fix(core polyfills): Pass a destination object with a url property al…
Browse files Browse the repository at this point in the history
…ong with the navigate event.
  • Loading branch information
thet committed Jan 16, 2025
1 parent b7b81ce commit 2301c73
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/core/polyfills.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,38 @@
// event on location change and even that without interception support, etc.
!(function () {
if (window.navigation == undefined) {

class NavigationEvent extends CustomEvent {
constructor() {
super("navigate");
this.destination = { url: undefined };
}
}

// Create a navigation object on the window
// We create a DOM element for the navigation object so that we can
// attach events on it.
window.navigation = document.createElement("div");

const create_event = (args) => {
const event = new NavigationEvent();
event.destination.url = args[2];
return event;
};

// Patch pushState to trigger an `navigate` event on the navigation
// object when the URL changes.
const pushState = window.history.pushState;
window.history.pushState = function () {
pushState.apply(window.history, arguments);
window.navigation.dispatchEvent(new Event("navigate"));
window.navigation.dispatchEvent(create_event(arguments));
};

// Same with replaceState
const replaceState = window.history.replaceState;
window.history.replaceState = function () {
replaceState.apply(window.history, arguments);
window.navigation.dispatchEvent(new Event("navigate"));
window.navigation.dispatchEvent(create_event(arguments));
};
}
})();

0 comments on commit 2301c73

Please sign in to comment.