Skip to content

Commit 358514e

Browse files
committed
add beforepush beforereplace event
1 parent 9fe8b83 commit 358514e

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

router/index.tsx

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,23 @@ const events = [eventPopstate, eventPushState, eventReplaceState];
208208
if (typeof history !== "undefined") {
209209
for (const type of [eventPushState, eventReplaceState] as const) {
210210
const original = history[type];
211-
history[type] = function (...args: Parameters<typeof original>) {
212-
const result = original.apply(this, args);
213-
const event = new Event(type);
211+
history[type] = function (
212+
data: any,
213+
unused: string,
214+
url?: string | URL | null | undefined
215+
) {
216+
const payload = { data, url };
217+
const before = new CustomEvent("before" + type, {
218+
detail: payload,
219+
cancelable: true,
220+
});
221+
let canceled = false;
222+
unstable_batchedUpdates(() => {
223+
canceled = dispatchEvent(before);
224+
});
225+
if (canceled) return;
226+
const result = original.call(this, payload.data, unused, payload.url);
227+
const event = new CustomEvent(type);
214228
unstable_batchedUpdates(() => {
215229
dispatchEvent(event);
216230
});

0 commit comments

Comments
 (0)