Skip to content

docs: clarify cacheLife() cannot be called at module scope#92326

Merged
aurorascharff merged 6 commits into
canaryfrom
docs/cachelife-module-scope-callout
Apr 8, 2026
Merged

docs: clarify cacheLife() cannot be called at module scope#92326
aurorascharff merged 6 commits into
canaryfrom
docs/cachelife-module-scope-callout

Conversation

@aurorascharff

Copy link
Copy Markdown
Contributor

What?

Update the "Good to know" callout in the cacheLife docs to explicitly state that cacheLife() must be called inside a function body, not at module scope, and that doing so throws an error.

Why?

Calling cacheLife('days') at module scope in a file with 'use cache' at the top throws: `cacheLife()` can only be called inside a "use cache" function. This is technically correct (module scope is not inside a function), but the previous docs only said cacheLife "should be placed within the function" without mentioning it errors. Both developers and AI agents hit this when they place cacheLife() next to the file-level 'use cache' directive at the top of the module.

How?

Replaced the soft "should be placed within the function" language with explicit "must be called inside a function body, not at module scope" and noted that it throws an error otherwise.

PR checklist (Improving Documentation)

Made with Cursor

@nextjs-bot nextjs-bot added created-by: Next.js DevEx team PRs by the DX team. Documentation Related to Next.js' official documentation. labels Apr 3, 2026
@aurorascharff aurorascharff marked this pull request as ready for review April 3, 2026 21:46
Copilot AI review requested due to automatic review settings April 3, 2026 21:46

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the cacheLife API reference documentation to explicitly state that cacheLife() must be called inside a function body (not at module scope), and that calling it at the top level will throw an error, even if 'use cache' is declared at file scope.

Changes:

  • Strengthened the “Good to know” guidance from “should be placed within the function” to “must be called inside a function body.”
  • Documented that module-scope usage throws an error and clarified the relationship to file-level 'use cache'.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread docs/01-app/03-api-reference/04-functions/cacheLife.mdx Outdated
Comment thread docs/01-app/03-api-reference/04-functions/cacheLife.mdx Outdated
@aurorascharff aurorascharff requested review from icyJoseph and ijjk April 7, 2026 10:10
Comment thread docs/01-app/03-api-reference/04-functions/cacheLife.mdx Outdated
Co-authored-by: Joseph <joseph.chamochumbi@vercel.com>
@aurorascharff aurorascharff removed the request for review from ijjk April 8, 2026 13:26
@aurorascharff aurorascharff merged commit 743615b into canary Apr 8, 2026
74 checks passed
@aurorascharff aurorascharff deleted the docs/cachelife-module-scope-callout branch April 8, 2026 14:06
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Apr 24, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

created-by: Next.js DevEx team PRs by the DX team. Documentation Related to Next.js' official documentation. locked

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants