Skip to content
Open
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/silent-streets-rest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/nextjs': patch
---

Fix prerendering errors when using `auth()` with Next.js 16 Cache Components.
17 changes: 17 additions & 0 deletions packages/nextjs/src/app-router/server/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,23 @@ export const auth: AuthFn = (async (options?: AuthOptions) => {
// eslint-disable-next-line @typescript-eslint/no-require-imports
require('server-only');

// Call connection() first to explicitly opt out of prerendering when using Next.js 16 Cache Components
// This prevents "During prerendering, headers() rejects" errors during build
try {
// @ts-expect-error: connection() only exists in Next.js 16+
const { connection } = await import('next/server');
if (typeof connection === 'function') {
await connection();
}
} catch (e) {
// If this is a prerendering bailout, re-throw it
const { isPrerenderingBailout } = await import('./utils.js');
if (isPrerenderingBailout(e)) {
throw e;
}
// Otherwise connection() doesn't exist in older Next.js versions, that's fine
}

const request = await buildRequestLike();

const stepsBasedOnSrcDirectory = async () => {
Expand Down
Loading