nextjs - 💡(How to fix) Fix Docs: How to "use cache" but only at runtime (not build) [9 comments, 4 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
vercel/next.js#86686Fetched 2026-04-08 02:09:48
View on GitHub
Comments
9
Participants
4
Timeline
29
Reactions
0
Author
Timeline (top)
subscribed ×13commented ×9mentioned ×5issue_type_added ×1
RAW_BUFFERClick to expand / collapse

What is the documentation issue?

I want to "use cache" some DB queries (in memory), but I absolutely don't want them to be pre-rendered during the build (my build environment doesn't even have access to the database). How do I exclude those from prerendering?

I initially thought I could await connection() but it doesn't work inside "use cache".

Is there any context that might help us understand?

Does the docs page already exist? Please link to it.

https://nextjs.org/docs/app/getting-started/cache-components

extent analysis

TL;DR

  • To exclude DB queries from prerendering, consider using a dynamic approach that fetches data at request time instead of build time.

Guidance

  • Review the cache components documentation to understand how caching works in Next.js and identify potential hooks for customizing the caching behavior.
  • Investigate using getStaticProps or getServerSideProps as alternatives to "use cache" for handling DB queries, as these methods allow for more control over when data is fetched.
  • Consider implementing a check to determine if the code is running in a build environment or at request time, allowing you to conditionally exclude DB queries from prerendering.
  • Look into using environment variables or other configuration options to control the behavior of your caching and DB query logic.

Example

// Example of conditionally fetching data based on environment
if (process.env.NODE_ENV !== 'production') {
  // Fetch data dynamically
} else {
  // Handle caching or prerendering for production
}

Notes

  • The provided solution assumes that the build environment can be distinguished from the runtime environment, which may not always be the case.
  • The example code snippet is hypothetical and may require adjustments based on the actual implementation details.

Recommendation

  • Apply workaround: Use a dynamic data fetching approach, such as getServerSideProps, to exclude DB queries from prerendering during the build process, as this allows for more control over when data is fetched.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

nextjs - 💡(How to fix) Fix Docs: How to "use cache" but only at runtime (not build) [9 comments, 4 participants]