Skip to content

Commit

Permalink
add beforepush beforereplace event
Browse files Browse the repository at this point in the history
  • Loading branch information
codehz committed Apr 29, 2024
1 parent 9fe8b83 commit 358514e
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions router/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,23 @@ const events = [eventPopstate, eventPushState, eventReplaceState];
if (typeof history !== "undefined") {
for (const type of [eventPushState, eventReplaceState] as const) {
const original = history[type];
history[type] = function (...args: Parameters<typeof original>) {
const result = original.apply(this, args);
const event = new Event(type);
history[type] = function (
data: any,
unused: string,
url?: string | URL | null | undefined
) {
const payload = { data, url };
const before = new CustomEvent("before" + type, {
detail: payload,
cancelable: true,
});
let canceled = false;
unstable_batchedUpdates(() => {
canceled = dispatchEvent(before);
});
if (canceled) return;
const result = original.call(this, payload.data, unused, payload.url);
const event = new CustomEvent(type);
unstable_batchedUpdates(() => {
dispatchEvent(event);
});
Expand Down

0 comments on commit 358514e

Please sign in to comment.