Skip to content

Commit 184528d

Browse files
author
Jacek
committed
test(nextjs): Add E2E tests for dynamic route with cacheComponents
1 parent f82abb2 commit 184528d

3 files changed

Lines changed: 22 additions & 5 deletions

File tree

‎.changeset/fix-usepathname-cache-components.md‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,4 @@
22
'@clerk/nextjs': patch
33
---
44

5-
Remove `usePathname()` from internal navigation hook for `cacheComponents` compatibility.
6-
7-
`usePathname()` was called in `useInternalNavFun` (used by the internal `useAwaitablePush`/`useAwaitableReplace` hooks in `ClientClerkProvider`). With Next.js 16's `cacheComponents: true`, `usePathname()` is treated as uncached/dynamic data during prerendering. Since `ClerkProvider` is rendered in the layout (outside `<Suspense>`), this caused build failures on routes with dynamic segments (e.g. `[id]`).
8-
9-
The fix removes `usePathname()` and relies on `useTransition`'s `isPending` state to detect navigation completion, which is sufficient for flushing pending navigation promises.
5+
Fix build failures when using `cacheComponents: true` with dynamic routes (e.g. `[id]`)

‎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

0 commit comments

Comments
 (0)