Skip to content

Commit

Permalink
fix(use-router): router.push bug (#1067)
Browse files Browse the repository at this point in the history
fix #1060

---------

Co-authored-by: Tyler <[email protected]>
Co-authored-by: daishi <[email protected]>
  • Loading branch information
3 people authored Dec 14, 2024
1 parent 4eb1bae commit d4849ba
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 10 deletions.
2 changes: 1 addition & 1 deletion e2e/fixtures/use-router/src/TestRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default function TestRouter() {
const router = useRouter_UNSTABLE();
const params = new URLSearchParams(router.query);
const queryCount = parseInt(params.get('count') || '0');
const hashCount = parseInt(router.hash?.substr(1) || '0');
const hashCount = parseInt(router.hash?.slice(1) || '0');
return (
<>
<p data-testid="path">Path: {router.path}</p>
Expand Down
12 changes: 12 additions & 0 deletions e2e/fixtures/use-router/src/components/my-button.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use client';

import { useRouter_UNSTABLE } from 'waku';

export const MyButton = () => {
const router = useRouter_UNSTABLE();
return (
<button onClick={() => router.push(`/static`)}>
Static router.push button
</button>
);
};
3 changes: 3 additions & 0 deletions e2e/fixtures/use-router/src/pages/dynamic.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { Link } from 'waku';
import TestRouter from '../TestRouter.js';

import { MyButton } from '../components/my-button.js';

const Page = () => (
<>
<h1>Dynamic</h1>
<p>
<Link to="/static">Go to static</Link>
<MyButton />
</p>
<TestRouter />
</>
Expand Down
8 changes: 8 additions & 0 deletions e2e/use-router.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ test.describe('useRouter', async () => {
await expect(page.getByTestId('path')).toHaveText('Path: /dynamic');
await terminate(pid!);
});
test('router.push changes the page', async ({ page }) => {
const [port, pid] = await start();
await page.goto(`http://localhost:${port}/dynamic`);
await page.click('text=Static router.push button');
await expect(page.getByRole('heading', { name: 'Static' })).toBeVisible();
await expect(page.getByTestId('path')).toHaveText('Path: /static');
await terminate(pid!);
});
});

test.describe('retrieves query variables', () => {
Expand Down
15 changes: 6 additions & 9 deletions packages/waku/src/router/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,17 +354,14 @@ const InnerRouter = ({
(route, options) => {
const { skipRefetch } = options || {};
startTransition(() => {
if (!staticPathSet.has(route.path) && !skipRefetch) {
const skip = Array.from(cachedIdSet);
const rscPath = encodeRoutePath(route.path);
const rscParams = createRscParams(route.query, skip);
refetch(rscPath, rscParams);
}
setRoute(route);
});
if (staticPathSet.has(route.path)) {
return;
}
if (!skipRefetch) {
const skip = Array.from(cachedIdSet);
const rscPath = encodeRoutePath(route.path);
const rscParams = createRscParams(route.query, skip);
refetch(rscPath, rscParams);
}
},
[refetch, cachedIdSet, staticPathSet],
);
Expand Down

0 comments on commit d4849ba

Please sign in to comment.