Skip to content

Commit

Permalink
feat: add disableSameURL option to router.push
Browse files Browse the repository at this point in the history
  • Loading branch information
Skyleen77 committed Apr 1, 2024
1 parent 4188aad commit 8220c71
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ router.back(NProgressOptions?: RouterNProgressOptions)
interface RouterNProgressOptions {
showProgressBar?: boolean;
startPosition?: number;
disableSameURL?: boolean;
}
```

Expand Down
9 changes: 9 additions & 0 deletions demo/app-dir/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ export default function Home() {
Link with nprogress disabled
</Link>
<button onClick={() => router.push('/dashboard')}>Push Dashboard</button>
<button
onClick={() =>
router.push('/', undefined, {
disableSameURL: false,
})
}
>
Push Dashboard with disableSameURL
</button>
<button className="replace" onClick={() => router.replace('/dashboard')}>
Replace Dashboard
</button>
Expand Down
3 changes: 3 additions & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ export interface ProgressBarProps {
export interface RouterNProgressOptions {
showProgressBar?: boolean;
startPosition?: number;
disableSameURL?: boolean;
}
export declare const startProgress: () => void;
export declare const stopProgress: (force?: boolean) => void;
declare const AppProgressBar: (props: ProgressBarProps) => import("react").JSX.Element;
export { AppProgressBar, useRouter };
export { PagesProgressBar } from './PagesProgressBar';
13 changes: 11 additions & 2 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "next-nprogress-bar",
"version": "2.3.10",
"version": "2.3.11",
"description": "NextJS progress bar compatible with new app directory",
"repository": {
"type": "git",
Expand Down
6 changes: 5 additions & 1 deletion src/AppProgressBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,11 @@ export function useRouter() {
const currentUrl = new URL(location.href);
const targetUrl = new URL(href, location.href);

if (isSameURL(targetUrl, currentUrl)) return router.push(href, options);
if (
isSameURL(targetUrl, currentUrl) &&
NProgressOptions?.disableSameURL !== false
)
return router.push(href, options);

startProgress(NProgressOptions?.startPosition);
},
Expand Down
10 changes: 10 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { start, done } from 'nprogress';
import {
AppProgressBar as AppProgressBarComponent,
useRouter,
Expand Down Expand Up @@ -50,8 +51,17 @@ export interface ProgressBarProps {
export interface RouterNProgressOptions {
showProgressBar?: boolean;
startPosition?: number;
disableSameURL?: boolean;
}

export const startProgress = () => {
start();
};

export const stopProgress = (force?: boolean) => {
done(force);
};

const AppProgressBar = withSuspense<ProgressBarProps>(AppProgressBarComponent);
export { AppProgressBar, useRouter };
export { PagesProgressBar } from './PagesProgressBar';

0 comments on commit 8220c71

Please sign in to comment.