feat(nextjs): Add support for Next.js 16 cache components#7595
feat(nextjs): Add support for Next.js 16 cache components#7595jacekradko merged 13 commits intomainfrom
Conversation
- Add `isNextjsUseCacheError()` helper to detect "use cache" context errors - Update `isPrerenderingBailout()` to detect cache component prerendering errors - Update `buildRequestLike()` to provide helpful error message with code example when auth() is called inside a "use cache" function - Update `clerkClient()` to re-throw "use cache" errors - Add unit tests for error detection helpers
🦋 Changeset detectedLatest commit: 9db278b The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
📝 WalkthroughWalkthroughAdds explicit detection and handling for Next.js “use cache” / cache components errors to the Clerk Next.js package. Introduces 🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Comment |
@clerk/agent-toolkit
@clerk/astro
@clerk/backend
@clerk/chrome-extension
@clerk/clerk-js
@clerk/dev-cli
@clerk/expo
@clerk/expo-passkeys
@clerk/express
@clerk/fastify
@clerk/localizations
@clerk/nextjs
@clerk/nuxt
@clerk/react
@clerk/react-router
@clerk/shared
@clerk/tanstack-react-start
@clerk/testing
@clerk/ui
@clerk/upgrade
@clerk/vue
commit: |
Wrap auth() and currentUser() in try-catch to catch "use cache" errors that bubble up from the Next.js cache boundary. The original implementation only caught errors inside buildRequestLike(), but Next.js throws these errors at a higher level.
Co-authored-by: Fredrik Höglund <fredrik@clerk.dev>
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@packages/nextjs/src/app-router/server/auth.ts`:
- Around line 138-143: The catch block in auth.ts re-wraps Next.js "use cache"
errors that buildRequestLike() already formats, causing duplicate messages;
update the catch in the function containing isNextjsUseCacheError(e) to first
detect if the thrown error is already formatted (e.g., check if e.message
startsWith or equals USE_CACHE_ERROR_MESSAGE or if a marker property exists) and
only re-wrap when it is not already formatted, or remove the catch entirely and
rely on buildRequestLike(); reference isNextjsUseCacheError, buildRequestLike,
and USE_CACHE_ERROR_MESSAGE when making the change.
- Add currentUser() server component test page and tests - Add currentUser() with "use cache" correct pattern page - Add error trigger page that calls auth() inside "use cache" - Add test verifying Clerk's custom error message is shown - Add signed-out state tests for cache and PPR pages - Update home page navigation with new test links
- Add ClerkUseCacheError class with symbol marker to identify already-formatted errors - Update auth() and currentUser() to check for ClerkUseCacheError before wrapping - Tighten isNextjsUseCacheError() regex to reduce false positives - Update clerkClient.ts to use isClerkUseCacheError
| }; | ||
|
|
||
| // Patterns for Next.js "use cache" errors - tightened to reduce false positives | ||
| const USE_CACHE_WITH_DYNAMIC_API_PATTERN = |
There was a problem hiding this comment.
❓ I think this regex will miss 'use cache' - shouldn't we catch this as well?
There was a problem hiding this comment.
Nice catch.. updated to handle both single and double quotes
…ponents-support # Conflicts: # packages/nextjs/src/app-router/server/auth.ts
Summary
Adds support for Next.js 16 cache components by improving error detection and providing helpful error messages when
auth()orcurrentUser()are called inside a"use cache"function.Changes
ClerkUseCacheErrorclass with symbol marker to prevent double-wrapping of error messagesisNextjsUseCacheError()helper to detect "use cache" context errors with tightened regex patterns to reduce false positivesisClerkUseCacheError()helper to identify already-formatted Clerk errorsbuildRequestLike()to throwClerkUseCacheErrorwith helpful messageauth()andcurrentUser()to check for already-formatted errors before wrappingclerkClient()to re-throwClerkUseCacheErrorProblem
When a developer calls
auth()orcurrentUser()inside a"use cache"function, they receive a cryptic error aboutheaders()not being allowed in cache contexts.Solution
Detect these errors and provide a helpful message with a code example showing the correct pattern:
Implementation details
ClerkUseCacheErrorclass with a symbol marker to prevent double-wrapping when errors bubble through multiple catch handlers (buildRequestLike→auth→currentUser)isNextjsUseCacheError()to reduce false positives - now requires specific Next.js error patterns like"use cache"combined withheaders/cookiescontextTest plan
ClerkUseCacheError,isClerkUseCacheError(),isNextjsUseCacheError(), andisPrerenderingBailout()How to verify
Setup
Create or use a Next.js 16 app with cache components enabled:
Install the PR preview package:
Test the error detection
Create a page that calls
auth()inside a"use cache"function:Expected behavior
Before this PR: Cryptic Next.js error about
headers()not being allowed in cache contextsAfter this PR: Clear error message explaining the issue and showing the correct pattern
Summary by CodeRabbit
New Features
Bug Fixes
Documentation
Tests