Skip to content

Commit

Permalink
fix:修复复写方法时拿到的不是最新的引用 (#700)
Browse files Browse the repository at this point in the history
Co-authored-by: guobiao.xgb <guobiao.xgb@alibaba-inc.com>
  • Loading branch information
beyondxgb and guobiao.xgb authored Sep 21, 2023
1 parent f4ba5c6 commit 9c1f6d0
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions packages/icestark/src/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ interface OriginalStateFunction {
}

let started = false;
const originalPush: OriginalStateFunction = window.history.pushState;
const originalReplace: OriginalStateFunction = window.history.replaceState;
const originalAddEventListener = window.addEventListener;
const originalRemoveEventListener = window.removeEventListener;
let originalPush: OriginalStateFunction;
let originalReplace: OriginalStateFunction;
let originalAddEventListener;
let originalRemoveEventListener;

const handleStateChange = (event: PopStateEvent, url: string, method: RouteType) => {
setHistoryEvent(event);
Expand Down Expand Up @@ -92,6 +92,9 @@ export function reroute(url: string, type: RouteType | 'init' | 'popstate'| 'has
* Hijack window.history
*/
const hijackHistory = (): void => {
originalPush = window.history.pushState;
originalReplace = window.history.replaceState;

Check failure on line 97 in packages/icestark/src/start.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Trailing spaces not allowed
window.history.pushState = (state: any, title: string, url?: string, ...rest) => {
originalPush.apply(window.history, [state, title, url, ...rest]);
const eventName = 'pushState';
Expand Down Expand Up @@ -123,6 +126,9 @@ const unHijackHistory = (): void => {
* Hijack eventListener
*/
const hijackEventListener = (): void => {
originalAddEventListener = window.addEventListener;
originalRemoveEventListener = window.removeEventListener;

window.addEventListener = (eventName, fn, ...rest) => {
if (
typeof fn === 'function' &&
Expand Down

0 comments on commit 9c1f6d0

Please sign in to comment.