diff --git a/src/index.tsx b/src/index.tsx index 66b8607..12b0936 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -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(); @@ -145,6 +156,8 @@ const HolyLoader = ({ stopProgress(); originalReplaceState(...args); }; + + isHistoryPatched = true; }; /** @@ -173,7 +186,6 @@ const HolyLoader = ({ } startProgress(); - stopProgressOnHistoryUpdate(); } catch (error) { stopProgress(); } @@ -192,6 +204,7 @@ const HolyLoader = ({ }); document.addEventListener('click', handleClick); + stopProgressOnHistoryUpdate(); } catch (error) {} return () => {