diff --git a/.changeset/friendly-sloths-travel.md b/.changeset/friendly-sloths-travel.md new file mode 100644 index 00000000000..40862fd0228 --- /dev/null +++ b/.changeset/friendly-sloths-travel.md @@ -0,0 +1,5 @@ +--- +'@clerk/nextjs': patch +--- + +Fix issue within `` where the window object is possibly undefined. diff --git a/packages/nextjs/src/app-router/client/ClerkProvider.tsx b/packages/nextjs/src/app-router/client/ClerkProvider.tsx index 3faaaefb26c..36cd10b4406 100644 --- a/packages/nextjs/src/app-router/client/ClerkProvider.tsx +++ b/packages/nextjs/src/app-router/client/ClerkProvider.tsx @@ -49,8 +49,9 @@ const useNextRouter = (): ClerkHostRouter => { shallowPush(path: string) { canUseWindowHistoryAPIs ? window.history.pushState(null, '', path) : router.push(path, {}); }, - pathname: () => window.location.pathname, - searchParams: () => new URLSearchParams(window.location.search), + pathname: () => (typeof window !== 'undefined' ? window.location.pathname : ''), + searchParams: () => + typeof window !== 'undefined' ? new URLSearchParams(window.location.search) : new URLSearchParams(), }; }; diff --git a/packages/nextjs/src/pages/ClerkProvider.tsx b/packages/nextjs/src/pages/ClerkProvider.tsx index 1da9a7c7259..4e1bb09948e 100644 --- a/packages/nextjs/src/pages/ClerkProvider.tsx +++ b/packages/nextjs/src/pages/ClerkProvider.tsx @@ -40,8 +40,9 @@ const useNextRouter = (): ClerkHostRouter => { shallowPush(path: string) { canUseWindowHistoryAPIs ? window.history.pushState(null, '', path) : router.push(path, {}); }, - pathname: () => window.location.pathname, - searchParams: () => new URLSearchParams(window.location.search), + pathname: () => (typeof window !== 'undefined' ? window.location.pathname : ''), + searchParams: () => + typeof window !== 'undefined' ? new URLSearchParams(window.location.search) : new URLSearchParams(), }; };