Skip to content

Commit 3157c25

Browse files
author
Jacek Radko
authored
fix(nextjs): Remove usePathname from navigation hook for cacheComponents compat (#7989)
1 parent 90f7130 commit 3157c25

7 files changed

Lines changed: 39 additions & 23 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clerk/nextjs': patch
3+
---
4+
5+
Fix build failures when using `cacheComponents: true` with dynamic routes (e.g. `[id]`)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export default async function DynamicPage({ params }: { params: Promise<{ id: string }> }) {
2+
const { id } = await params;
3+
return (
4+
<main>
5+
<h1>Dynamic Route</h1>
6+
<p data-testid='route-id'>{id}</p>
7+
</main>
8+
);
9+
}

‎integration/templates/next-cache-components/src/app/layout.tsx‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import './globals.css';
22
import { ClerkProvider } from '@clerk/nextjs';
3-
import { Suspense } from 'react';
43

54
export const metadata = {
65
title: 'Next.js Cache Components Test',
@@ -11,9 +10,7 @@ export default function RootLayout({ children }: { children: React.ReactNode })
1110
return (
1211
<html lang='en'>
1312
<body>
14-
<Suspense fallback={<div>Loading...</div>}>
15-
<ClerkProvider>{children}</ClerkProvider>
16-
</Suspense>
13+
<ClerkProvider>{children}</ClerkProvider>
1714
</body>
1815
</html>
1916
);

‎integration/templates/next-cache-components/src/app/page.tsx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ export default function Home() {
4242
<li>
4343
<Link href='/sign-in'>Sign In</Link>
4444
</li>
45+
<li>
46+
<Link href='/dynamic-route/test-123'>Dynamic Route</Link>
47+
</li>
4548
</ul>
4649
</nav>
4750
</main>

‎integration/tests/cache-components.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,24 @@ testAgainstRunningApps({ withEnv: [appConfigs.envs.withEmailCodes], withPattern:
284284
await expect(page).toHaveURL(/sign-in/);
285285
});
286286

287+
test('dynamic route renders correctly via direct navigation', async ({ page, context }) => {
288+
const u = createTestUtils({ app, page, context });
289+
await u.page.goToRelative('/dynamic-route/test-123');
290+
await expect(u.page.getByText('Dynamic Route')).toBeVisible();
291+
await expect(u.page.getByTestId('route-id')).toContainText('test-123');
292+
});
293+
294+
test('client-side navigation to dynamic route works', async ({ page, context }) => {
295+
const u = createTestUtils({ app, page, context });
296+
await u.page.goToRelative('/');
297+
await expect(u.page.getByText('Next.js Cache Components Test App')).toBeVisible();
298+
299+
// Click the dynamic route link (exercises ClerkProvider's navigation hooks)
300+
await u.page.getByRole('link', { name: 'Dynamic Route' }).click();
301+
await expect(u.page.getByText('Dynamic Route')).toBeVisible();
302+
await expect(u.page.getByTestId('route-id')).toContainText('test-123');
303+
});
304+
287305
test('protected route accessible when authenticated', async ({ page, context }) => {
288306
const u = createTestUtils({ app, page, context });
289307

‎packages/nextjs/src/app-router/client/__tests__/useInternalNavFun.test.tsx‎

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
44

55
import { useInternalNavFun } from '../useInternalNavFun';
66

7-
let mockedPathname = '/';
8-
9-
vi.mock('next/navigation', () => ({
10-
usePathname: () => mockedPathname,
11-
}));
12-
137
let currentNavigate: NavigationFunction | undefined;
148

159
const windowNav = window.history.pushState.bind(window.history);
@@ -34,7 +28,6 @@ const navigate = (to: string) => {
3428

3529
describe('useInternalNavFun', () => {
3630
beforeEach(() => {
37-
mockedPathname = '/protected';
3831
currentNavigate = undefined;
3932
window.__clerk_internal_navigations = {};
4033
vi.clearAllMocks();
@@ -46,7 +39,7 @@ describe('useInternalNavFun', () => {
4639

4740
it('dedupes duplicate in-flight pushes to the same destination', async () => {
4841
const routerNav = vi.fn();
49-
const view = render(<Harness routerNav={routerNav} />);
42+
render(<Harness routerNav={routerNav} />);
5043

5144
let firstPromise!: Promise<void>;
5245
let secondPromise!: Promise<void>;
@@ -60,10 +53,6 @@ describe('useInternalNavFun', () => {
6053
expect(routerNav).toHaveBeenCalledTimes(1);
6154
});
6255

63-
// Simulate route change completion so buffered resolvers flush.
64-
mockedPathname = '/sign-in';
65-
view.rerender(<Harness routerNav={routerNav} />);
66-
6756
await expect(Promise.all([firstPromise, secondPromise])).resolves.toEqual([undefined, undefined]);
6857
});
6958

@@ -86,7 +75,7 @@ describe('useInternalNavFun', () => {
8675

8776
it('allows a fresh same-destination push after flush', async () => {
8877
const routerNav = vi.fn();
89-
const view = render(<Harness routerNav={routerNav} />);
78+
render(<Harness routerNav={routerNav} />);
9079

9180
let firstPromise!: Promise<void>;
9281
let secondPromise!: Promise<void>;
@@ -100,9 +89,6 @@ describe('useInternalNavFun', () => {
10089
expect(routerNav).toHaveBeenCalledTimes(1);
10190
});
10291

103-
mockedPathname = '/sign-in';
104-
view.rerender(<Harness routerNav={routerNav} />);
105-
10692
await expect(Promise.all([firstPromise, secondPromise])).resolves.toEqual([undefined, undefined]);
10793

10894
act(() => {

‎packages/nextjs/src/app-router/client/useInternalNavFun.ts‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { AppRouterInstance } from 'next/dist/shared/lib/app-router-context.shared-runtime';
2-
import { usePathname } from 'next/navigation';
32
import { useCallback, useEffect, useTransition } from 'react';
43

54
import { removeBasePath } from '../../utils/removeBasePath';
@@ -17,7 +16,6 @@ export const useInternalNavFun = (props: {
1716
name: string;
1817
}): NavigationFunction => {
1918
const { windowNav, routerNav, name } = props;
20-
const pathname = usePathname();
2119
const [isPending, startTransition] = useTransition();
2220

2321
if (windowNav) {
@@ -81,7 +79,7 @@ export const useInternalNavFun = (props: {
8179
if (!isPending) {
8280
flushPromises();
8381
}
84-
}, [pathname, isPending]);
82+
}, [isPending]);
8583

8684
return useCallback<NavigationFunction>((to, metadata) => {
8785
return getClerkNavigationObject(name).fun(to, metadata);

0 commit comments

Comments
 (0)