diff --git a/.changeset/lucky-olives-explode.md b/.changeset/lucky-olives-explode.md new file mode 100644 index 00000000000..81321e8044e --- /dev/null +++ b/.changeset/lucky-olives-explode.md @@ -0,0 +1,5 @@ +--- +"@clerk/nextjs": patch +--- + +Fixes regression on `@clerk/nextjs/server` (introduced on https://github.com/clerk/javascript/pull/3758) where `server-only` module was being resolved in runtimes without `react-server` available, such as `getAuth` on `getServerSideProps`. diff --git a/packages/nextjs/src/app-router/server/auth.ts b/packages/nextjs/src/app-router/server/auth.ts index 836a27ae091..e90f46013d1 100644 --- a/packages/nextjs/src/app-router/server/auth.ts +++ b/packages/nextjs/src/app-router/server/auth.ts @@ -1,5 +1,3 @@ -import 'server-only'; - import type { AuthObject, RedirectFun } from '@clerk/backend/internal'; import { constants, createClerkRequest, createRedirect } from '@clerk/backend/internal'; import { notFound, redirect } from 'next/navigation'; @@ -16,6 +14,8 @@ import { buildRequestLike } from './utils'; type Auth = AuthObject & { protect: AuthProtect; redirectToSignIn: RedirectFun> }; export const auth = (): Auth => { + require('server-only'); + const request = buildRequestLike(); const authObject = createGetAuth({ debugLoggerName: 'auth()', diff --git a/packages/nextjs/src/app-router/server/currentUser.ts b/packages/nextjs/src/app-router/server/currentUser.ts index b73d8cc7087..336fd655658 100644 --- a/packages/nextjs/src/app-router/server/currentUser.ts +++ b/packages/nextjs/src/app-router/server/currentUser.ts @@ -1,11 +1,11 @@ -import 'server-only'; - import type { User } from '@clerk/backend'; import { clerkClient } from '../../server/clerkClient'; import { auth } from './auth'; export async function currentUser(): Promise { + require('server-only'); + const { userId } = auth(); if (!userId) { return null;