Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/giant-dingos-collect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/nextjs': patch
---

Use pathname and searchParams from next/navigation.
9 changes: 5 additions & 4 deletions packages/nextjs/src/app-router/client/ClerkProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { ClerkProvider as ReactClerkProvider } from '@clerk/clerk-react';
import type { ClerkHostRouter } from '@clerk/shared/router';
import { ClerkHostRouterContext } from '@clerk/shared/router';
import { useRouter } from 'next/navigation';
import { usePathname, useRouter, useSearchParams } from 'next/navigation';
import React, { useEffect, useTransition } from 'react';

import { useSafeLayoutEffect } from '../../client-boundary/hooks/useSafeLayoutEffect';
Expand Down Expand Up @@ -34,6 +34,8 @@ const NEXT_WINDOW_HISTORY_SUPPORT_VERSION = '14.1.0';
*/
const useNextRouter = (): ClerkHostRouter => {
const router = useRouter();
const pathname = usePathname();
const searchParams = useSearchParams();

// The window.history APIs seem to prevent Next.js from triggering a full page re-render, allowing us to
// preserve internal state between steps.
Expand All @@ -49,9 +51,8 @@ const useNextRouter = (): ClerkHostRouter => {
shallowPush(path: string) {
canUseWindowHistoryAPIs ? window.history.pushState(null, '', path) : router.push(path, {});
},
pathname: () => (typeof window !== 'undefined' ? window.location.pathname : ''),
searchParams: () =>
typeof window !== 'undefined' ? new URLSearchParams(window.location.search) : new URLSearchParams(),
pathname: () => pathname,
searchParams: () => searchParams,
};
};

Expand Down
8 changes: 5 additions & 3 deletions packages/nextjs/src/pages/ClerkProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ClerkProvider as ReactClerkProvider } from '@clerk/clerk-react';
// Override Clerk React error thrower to show that errors come from @clerk/nextjs
import { setClerkJsLoadingErrorPackageName, setErrorThrowerOptions } from '@clerk/clerk-react/internal';
import type { ClerkHostRouter } from '@clerk/shared/router';
import { usePathname, useSearchParams } from 'next/navigation';
import { useRouter } from 'next/router';
import React from 'react';

Expand All @@ -25,6 +26,8 @@ const NEXT_WINDOW_HISTORY_SUPPORT_VERSION = '14.1.0';
*/
const useNextRouter = (): ClerkHostRouter => {
const router = useRouter();
const pathname = usePathname();
const searchParams = useSearchParams();

// The window.history APIs seem to prevent Next.js from triggering a full page re-render, allowing us to
// preserve internal state between steps.
Expand All @@ -40,9 +43,8 @@ const useNextRouter = (): ClerkHostRouter => {
shallowPush(path: string) {
canUseWindowHistoryAPIs ? window.history.pushState(null, '', path) : router.push(path, {});
},
pathname: () => (typeof window !== 'undefined' ? window.location.pathname : ''),
searchParams: () =>
typeof window !== 'undefined' ? new URLSearchParams(window.location.search) : new URLSearchParams(),
pathname: () => pathname,
searchParams: () => searchParams,
};
};

Expand Down
Loading