From 8c92b79aea5dcd8723cb1af0ce771574bcfa8fa6 Mon Sep 17 00:00:00 2001 From: tomcru Date: Tue, 16 Jan 2024 20:58:24 +0100 Subject: [PATCH] fix: https://github.com/tomcru/holy-loader/issues/14 --- src/index.tsx | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/index.tsx b/src/index.tsx index 6721597..6acaa32 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -129,15 +129,22 @@ const HolyLoader = ({ }; /** - * Overrides the history.pushState function to stop the progress bar - * when navigating to a new page without a full page reload. + * Enhances browser history methods (pushState and replaceState) to ensure that the + * progress indicator is appropriately halted when navigating through single-page applications */ - const overridePushState = (): void => { + const stopProgressOnHistoryUpdate = (): void => { const originalPushState = history.pushState.bind(history); history.pushState = (...args) => { stopProgress(); originalPushState(...args); }; + + // This is crucial for Next.js Link components using the 'replace' prop. + const originalReplaceState = history.replaceState.bind(history); + history.replaceState = (...args) => { + stopProgress(); + originalReplaceState(...args); + }; }; /** @@ -166,7 +173,7 @@ const HolyLoader = ({ } startProgress(); - overridePushState(); + stopProgressOnHistoryUpdate(); } catch (error) { stopProgress(); }