-
Notifications
You must be signed in to change notification settings - Fork 30.4k
Require explicit cacheLife on outer "use cache" when nesting short-lived caches
#89481
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 all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f9eec9e
Initial implementation
unstubbable 09f5309
Ensure error stacks
unstubbable 15a1378
Implement low expire handling and update docs
unstubbable 893526d
Remove stale error message
unstubbable 99a179c
Update test snapshots
unstubbable 2803983
Add `hasExplicitExpire` to test files
unstubbable 7157c2e
Add positive test cases
unstubbable 3e273c3
Adjust fetch revalidate test case
unstubbable a75111a
Update test snapshots
unstubbable 819ae58
`/fetch-revalidate` is now static
unstubbable 7c7b1c2
Improve nested cacheLife examples in docs and error page
unstubbable 455941c
Add a note about remote caching
unstubbable 52ad6d6
Rephrase short-lived cache prerender note
unstubbable 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| --- | ||
| title: Nested `"use cache"` with short cache lifetime requires explicit `cacheLife` on outer cache | ||
| --- | ||
|
|
||
| ## Why This Error Occurred | ||
|
|
||
| A `"use cache"` function or component with a very short cache lifetime (either `revalidate: 0` or `expire` under 5 minutes) is nested inside another `"use cache"` that doesn't have an explicit `cacheLife()` call. | ||
|
|
||
| When a nested cache has a very short lifetime, it would normally create a "dynamic hole" - meaning it's excluded from static prerenders. However, when this happens inside another `"use cache"` without an explicit `cacheLife`, the outer cache's lifetime silently becomes very short too (via propagation), which may be unintentional. | ||
|
|
||
| To prevent accidental misconfigurations, Next.js requires you to explicitly declare your intent by adding `cacheLife()` to the outer `"use cache"`. | ||
|
|
||
| ## Possible Ways to Fix It | ||
|
|
||
| Add an explicit `cacheLife()` call to the outer `"use cache"` to declare your intent. | ||
|
|
||
| ### Before | ||
|
|
||
| ```jsx filename="components/short-lived-widget.js" | ||
| import { cacheLife } from 'next/cache' | ||
|
|
||
| export async function ShortLivedWidget() { | ||
| 'use cache' | ||
| cacheLife('seconds') | ||
| const data = await fetchRealtimeData() | ||
| return <div>{data}</div> | ||
| } | ||
| ``` | ||
|
|
||
| ```jsx filename="app/page.js" | ||
| import { ShortLivedWidget } from '@/components/short-lived-widget' | ||
|
|
||
| export default async function Page() { | ||
| 'use cache' | ||
| // Error: no explicit cacheLife on outer cache | ||
| return ( | ||
| <div> | ||
| <h1>Dashboard</h1> | ||
| <p>Last updated: {new Date().toISOString()}</p> | ||
| <ShortLivedWidget /> | ||
| </div> | ||
| ) | ||
| } | ||
| ``` | ||
|
|
||
| ### After: If you want the outer cache to remain static (prerendered) | ||
|
|
||
| Set a longer cache lifetime on the outer cache: | ||
|
|
||
| ```jsx filename="app/page.js" highlight={6} | ||
| import { cacheLife } from 'next/cache' | ||
| import { ShortLivedWidget } from '@/components/short-lived-widget' | ||
|
|
||
| export default async function Page() { | ||
| 'use cache' | ||
| cacheLife('default') // Explicit cacheLife prevents the error | ||
| return ( | ||
| <div> | ||
| <h1>Dashboard</h1> | ||
| <p>Last updated: {new Date().toISOString()}</p> | ||
| <ShortLivedWidget /> | ||
| </div> | ||
| ) | ||
| } | ||
| ``` | ||
|
|
||
| ### After: If you want the outer cache to also be short-lived | ||
|
|
||
| Explicitly set a short cache lifetime on the outer cache to confirm this is intentional. Wrap the component in a `<Suspense>` boundary to provide a fallback while content loads: | ||
|
|
||
| ```jsx filename="app/page.js" highlight={7,17-19} | ||
| import { Suspense } from 'react' | ||
| import { cacheLife } from 'next/cache' | ||
| import { ShortLivedWidget } from '@/components/short-lived-widget' | ||
|
|
||
| async function Content() { | ||
| 'use cache: remote' | ||
| cacheLife('seconds') // Explicit cacheLife confirms this is intentionally short-lived | ||
| return ( | ||
| <> | ||
| <p>Last updated: {new Date().toISOString()}</p> | ||
| <ShortLivedWidget /> | ||
| </> | ||
| ) | ||
| } | ||
|
|
||
| export default function Page() { | ||
| return ( | ||
| <div> | ||
| <h1>Dashboard</h1> | ||
| <Suspense fallback={<p>Loading...</p>}> | ||
| <Content /> | ||
| </Suspense> | ||
| </div> | ||
| ) | ||
| } | ||
| ``` | ||
|
|
||
| > **Note:** This example uses `"use cache: remote"` because runtime caching in serverless deployments doesn't persist across requests with the default in-memory cache. For self-hosted environments, `"use cache"` may be sufficient. See [Runtime caching considerations](/docs/app/api-reference/directives/use-cache#runtime-caching-considerations) for more details. | ||
|
|
||
| ## Useful Links | ||
|
|
||
| - [`cacheLife` function](/docs/app/api-reference/functions/cacheLife) | ||
| - [`"use cache"` directive](/docs/app/api-reference/directives/use-cache) | ||
| - [Prerendering behavior](/docs/app/api-reference/functions/cacheLife#prerendering-behavior) | ||
| - [Nested short-lived caches](/docs/app/api-reference/functions/cacheLife#nested-short-lived-caches) |
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.
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.