From c103979bc5f33f925e61b28c18e6473637709fc2 Mon Sep 17 00:00:00 2001 From: Alex Carpenter Date: Tue, 5 Nov 2024 14:58:45 -0500 Subject: [PATCH 1/2] fix(nextjs): Use next pathname and searchParams --- packages/nextjs/src/app-router/client/ClerkProvider.tsx | 9 +++++---- packages/nextjs/src/pages/ClerkProvider.tsx | 8 +++++--- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/packages/nextjs/src/app-router/client/ClerkProvider.tsx b/packages/nextjs/src/app-router/client/ClerkProvider.tsx index 36cd10b4406..bc70afe04f9 100644 --- a/packages/nextjs/src/app-router/client/ClerkProvider.tsx +++ b/packages/nextjs/src/app-router/client/ClerkProvider.tsx @@ -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'; @@ -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. @@ -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, }; }; diff --git a/packages/nextjs/src/pages/ClerkProvider.tsx b/packages/nextjs/src/pages/ClerkProvider.tsx index 4e1bb09948e..104c23e5220 100644 --- a/packages/nextjs/src/pages/ClerkProvider.tsx +++ b/packages/nextjs/src/pages/ClerkProvider.tsx @@ -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'; @@ -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. @@ -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, }; }; From 93f286c649d36e25585656c644015e2859ebe594 Mon Sep 17 00:00:00 2001 From: Alex Carpenter Date: Tue, 5 Nov 2024 14:59:57 -0500 Subject: [PATCH 2/2] add changeset --- .changeset/giant-dingos-collect.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/giant-dingos-collect.md diff --git a/.changeset/giant-dingos-collect.md b/.changeset/giant-dingos-collect.md new file mode 100644 index 00000000000..1edb3287cb3 --- /dev/null +++ b/.changeset/giant-dingos-collect.md @@ -0,0 +1,5 @@ +--- +'@clerk/nextjs': patch +--- + +Use pathname and searchParams from next/navigation.