Skip to content

Commit

Permalink
fix: fix #12 show progress bar on query param change
Browse files Browse the repository at this point in the history
  • Loading branch information
ndungtse committed Jan 1, 2024
1 parent 2d4ed85 commit bb7d57a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
15 changes: 12 additions & 3 deletions .github/workflows/npm-publish-github-packages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@

name: Node.js Package

# on:
# release:
# types: [created]
on:
release:
types: [created]
workflow_dispatch:
inputs:
branch:
description: 'Branch to deploy'
required: true
default: 'main'

jobs:
build:
Expand Down Expand Up @@ -34,6 +41,8 @@ jobs:
cache: 'yarn'
- run: yarn install
- run: yarn run prepack
# Authenticate with NPM to enable publishing

- run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
20 changes: 20 additions & 0 deletions example/src/app/search/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use client';

// import { useRouter } from '../../../../dist';
import { useRouter } from 'next13-progressbar';

const SearchPage = () => {
const router = useRouter();
return (
<div>
{[1, 2, 3, 4, 5].map((i) => (
<div key={i}>
{/* <Link href={`/search?item=${i}`}>Item {i}</Link> */}
<button onClick={() => router.push(`/search?item=${i}`)}>Item {i}</button>
</div>
))}
</div>
);
};

export default SearchPage;
1 change: 1 addition & 0 deletions example/src/components/NavBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const NavBar = () => {
<button onClick={() => router.push('/button-link#34')}>ButtonLink</button>
<Link href={'#'}>HashLink</Link>
<Link href={'/contact/#44'}>HashLink1</Link>
<Link href={'/search'}>Search</Link>
<button onClick={() => router.replace('/about')}>Replace</button>
</div>
);
Expand Down
11 changes: 9 additions & 2 deletions src/AppProgressBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,11 @@ export const Next13ProgressBar = React.memo(
// target url without hash removed
const targetUrl = new URL(anchorElement.href);
const currentUrl = new URL(location.href);
const isSameUrl = targetUrl?.pathname === currentUrl?.pathname;

// check if search params changed
const hasSearchParams = targetUrl?.searchParams?.toString() !== currentUrl?.searchParams?.toString();
const paramsChanged = hasSearchParams && targetUrl?.search !== currentUrl?.search;
const isSameUrl = targetUrl?.pathname === currentUrl?.pathname && !paramsChanged;

// detect ctrl/cmd option/alt shift click
if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return;
Expand Down Expand Up @@ -161,7 +165,10 @@ export function useRouter() {

function push(href: string, options?: NavigateOptions) {
const targetUrl = new URL(href, location.href);
if (targetUrl.pathname === pathname) return Promise.resolve(true);
const currentUrl = new URL(location.href);
const hasSearchParams = targetUrl?.searchParams?.toString() !== currentUrl?.searchParams?.toString();
const paramsChanged = hasSearchParams && targetUrl?.search !== currentUrl?.search;
if (targetUrl.pathname === pathname && !paramsChanged) return Promise.resolve(true);
NProgress.start();
return router.push(href, options);
}
Expand Down

0 comments on commit bb7d57a

Please sign in to comment.