-
Notifications
You must be signed in to change notification settings - Fork 453
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f5da1c6
feat(nextjs): Add support for Next.js 16 cache components
jacekradko 5a1d4c3
fix(nextjs): catch use cache errors at auth/currentUser level
jacekradko 16cb41b
refactor: hoist route bailout regex pattern to module level
jacekradko 376fd84
style: format utils.ts
jacekradko 3f20681
Update packages/nextjs/src/app-router/server/utils.ts
jacekradko e4f48fb
Merge branch 'main' into feat/nextjs-cache-components-support
jacekradko eed6aeb
Merge branch 'main' into feat/nextjs-cache-components-support
jacekradko db8d6de
test(nextjs): add E2E tests for cache components support
jacekradko 6fa6c93
fix(nextjs): prevent double-wrapping of use cache errors
jacekradko 1686fb2
Merge branch 'main' into feat/nextjs-cache-components-support
jacekradko 3404ed3
chore: format
jacekradko 697ab92
fix(nextjs): match both single and double quotes in use cache error d…
jacekradko 9db278b
Merge remote-tracking branch 'origin/main' into feat/nextjs-cache-com…
jacekradko 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
Some comments aren't visible on the classic Files Changed page.
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.
Uh oh!
There was an error while loading. Please reload this page.