-
Notifications
You must be signed in to change notification settings - Fork 424
feat(nextjs): Add support for Next.js 16 cache components #7595
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jacekradko
wants to merge
2
commits into
main
Choose a base branch
from
feat/nextjs-cache-components-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+238
−66
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@clerk/nextjs': patch | ||
| --- | ||
|
|
||
| Add support for Next.js 16 cache components by improving error detection and providing helpful error messages when `auth()` or `currentUser()` are called inside a `"use cache"` function. |
92 changes: 92 additions & 0 deletions
92
packages/nextjs/src/app-router/server/__tests__/utils.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
|
|
||
| import { isNextjsUseCacheError, isPrerenderingBailout } from '../utils'; | ||
|
|
||
| describe('isPrerenderingBailout', () => { | ||
| it('returns false for non-Error values', () => { | ||
| expect(isPrerenderingBailout(null)).toBe(false); | ||
| expect(isPrerenderingBailout(undefined)).toBe(false); | ||
| expect(isPrerenderingBailout('string')).toBe(false); | ||
| expect(isPrerenderingBailout(123)).toBe(false); | ||
| expect(isPrerenderingBailout({})).toBe(false); | ||
| }); | ||
|
|
||
| it('returns true for dynamic server usage errors', () => { | ||
| const error = new Error('Dynamic server usage: headers'); | ||
| expect(isPrerenderingBailout(error)).toBe(true); | ||
| }); | ||
|
|
||
| it('returns true for bail out of prerendering errors', () => { | ||
| const error = new Error('This page needs to bail out of prerendering'); | ||
| expect(isPrerenderingBailout(error)).toBe(true); | ||
| }); | ||
|
|
||
| it('returns true for route prerendering bailout errors (Next.js 14.1.1+)', () => { | ||
| const error = new Error( | ||
| 'Route /example needs to bail out of prerendering at this point because it used headers().', | ||
| ); | ||
| expect(isPrerenderingBailout(error)).toBe(true); | ||
| }); | ||
|
|
||
| it('returns true for headers() rejection during prerendering (Next.js 16 cacheComponents)', () => { | ||
| const error = new Error( | ||
| 'During prerendering, `headers()` rejects when the prerender is complete. ' + | ||
| 'Typically these errors are handled by React but if you move `headers()` to a different context ' + | ||
| 'by using `setTimeout`, `after`, or similar functions you may observe this error and you should handle it in that context.', | ||
| ); | ||
| expect(isPrerenderingBailout(error)).toBe(true); | ||
| }); | ||
|
|
||
| it('returns false for unrelated errors', () => { | ||
| const error = new Error('Some other error'); | ||
| expect(isPrerenderingBailout(error)).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe('isNextjsUseCacheError', () => { | ||
| it('returns false for non-Error values', () => { | ||
| expect(isNextjsUseCacheError(null)).toBe(false); | ||
| expect(isNextjsUseCacheError(undefined)).toBe(false); | ||
| expect(isNextjsUseCacheError('string')).toBe(false); | ||
| expect(isNextjsUseCacheError(123)).toBe(false); | ||
| expect(isNextjsUseCacheError({})).toBe(false); | ||
| }); | ||
|
|
||
| it('returns true for "use cache" errors', () => { | ||
| const error = new Error('Route /example used `headers()` inside "use cache"'); | ||
| expect(isNextjsUseCacheError(error)).toBe(true); | ||
| }); | ||
|
|
||
| it('returns true for cache scope errors', () => { | ||
| const error = new Error( | ||
| 'Accessing Dynamic data sources inside a cache scope is not supported. ' + | ||
| 'If you need this data inside a cached function use `headers()` outside of the cached function.', | ||
| ); | ||
| expect(isNextjsUseCacheError(error)).toBe(true); | ||
| }); | ||
|
|
||
| it('returns true for dynamic data source cache errors', () => { | ||
| const error = new Error('Dynamic data source accessed in cache context'); | ||
| expect(isNextjsUseCacheError(error)).toBe(true); | ||
| }); | ||
|
|
||
| it('returns false for regular prerendering bailout errors', () => { | ||
| const error = new Error('Dynamic server usage: headers'); | ||
| expect(isNextjsUseCacheError(error)).toBe(false); | ||
| }); | ||
|
|
||
| it('returns false for unrelated errors', () => { | ||
| const error = new Error('Some other error'); | ||
| expect(isNextjsUseCacheError(error)).toBe(false); | ||
| }); | ||
|
|
||
| it('returns true for the exact Next.js 16 error message', () => { | ||
| const error = new Error( | ||
| 'Route /examples/cached-components used `headers()` inside "use cache". ' + | ||
| 'Accessing Dynamic data sources inside a cache scope is not supported. ' + | ||
| 'If you need this data inside a cached function use `headers()` outside of the cached function ' + | ||
| 'and pass the required dynamic data in as an argument.', | ||
| ); | ||
| expect(isNextjsUseCacheError(error)).toBe(true); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: consider hoisting this pattern out of the function like the others in this file