Skip to content

Commit

Permalink
fix: #14
Browse files Browse the repository at this point in the history
  • Loading branch information
tomcru committed Jan 16, 2024
1 parent af730a0 commit 8c92b79
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
};

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

startProgress();
overridePushState();
stopProgressOnHistoryUpdate();
} catch (error) {
stopProgress();
}
Expand Down

0 comments on commit 8c92b79

Please sign in to comment.