Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,22 @@ const HolyLoader = ({
} catch (error) {}
};

/**
* Flag to prevent redundant patching of History API methods.
* This is essential to avoid pushState & replaceState increasingly nesting
* withing patched versions of itself
*/
let isHistoryPatched = false;

/**
* Enhances browser history methods (pushState and replaceState) to ensure that the
* progress indicator is appropriately halted when navigating through single-page applications
*/
const stopProgressOnHistoryUpdate = (): void => {
if (isHistoryPatched) {
return;
}

const originalPushState = history.pushState.bind(history);
history.pushState = (...args) => {
stopProgress();
Expand All @@ -145,6 +156,8 @@ const HolyLoader = ({
stopProgress();
originalReplaceState(...args);
};

isHistoryPatched = true;
};

/**
Expand Down Expand Up @@ -173,7 +186,6 @@ const HolyLoader = ({
}

startProgress();
stopProgressOnHistoryUpdate();
} catch (error) {
stopProgress();
}
Expand All @@ -192,6 +204,7 @@ const HolyLoader = ({
});

document.addEventListener('click', handleClick);
stopProgressOnHistoryUpdate();
} catch (error) {}

return () => {
Expand Down