Skip to content

Commit 063fe09

Browse files
committed
feat(core polyfills): Add polyfill for navigation API.
This polyfill adds support for the "navigate" event on the navigation object. We path "history.pushState" and "history.replaceState" to send the "navigate" event on the "window.navigation" object when the URL changes. This polyfill is for current Firefox and Safari. Chrome based browsers already support this. More information on: https://developer.mozilla.org/en-US/docs/Web/API/Navigation/navigate_event
1 parent 1fa0392 commit 063fe09

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/core/polyfills.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,30 @@
3333
true
3434
);
3535
})();
36+
37+
// Navigation polyfill for Firefox and Safari, as of 2024-01-04
38+
// NOTE: this is a very basic polyfill, it only supports firing a `navigate`
39+
// event on location change and even that without interception support, etc.
40+
!(function () {
41+
if (window.navigation == undefined) {
42+
// Create a navigation object on the window
43+
// We create a DOM element for the navigation object so that we can
44+
// attach events on it.
45+
window.navigation = document.createElement("div");
46+
47+
// Patch pushState to trigger an `navigate` event on the navigation
48+
// object when the URL changes.
49+
const pushState = window.history.pushState;
50+
window.history.pushState = function () {
51+
pushState.apply(window.history, arguments);
52+
window.navigation.dispatchEvent(new Event("navigate"));
53+
};
54+
55+
// Same with replaceState
56+
const replaceState = window.history.replaceState;
57+
window.history.replaceState = function () {
58+
replaceState.apply(window.history, arguments);
59+
window.navigation.dispatchEvent(new Event("navigate"));
60+
};
61+
}
62+
})();

0 commit comments

Comments
 (0)