use cache after connection #86417
Replies: 2 comments
-
|
You are not missing anything, you have actually discovered a nuance where the documentation simplifies the reality to encourage "correct" architectural patterns, even though the underlying mechanics are more flexible. Here is the breakdown of why your code works, why the docs say it shouldn't, and the subtle difference between 1. Why your code worksThe directive When you put Even though the Parent Component (
Therefore, the dynamic nature of the parent does not "infect" the cached function, provided the cached function itself does not try to access dynamic data (like reading headers inside the cached scope). 2. What the Docs mean by "Stops Working"When the documentation says:
They are simplifying the behavior of Implicit Caching vs. Explicit Directives.
However, the documentation is likely warning you about Static Generation (Build Time).
In short: Your code works, but it has shifted from "Prerendered Cache" (Build time) to "Just-in-Time Cache" (Runtime), which is exactly what 3. The Difference: |
Beta Was this translation helpful? Give feedback.
-
✅ Valid AnswerThe key detail is this:
This is the part that’s easy to misunderstand. ✔️ What actually happens under the hoodWhen you call: await connection();or await cookies();Next.js marks the route as dynamic, meaning:
But it does NOT stop the server-side cache handlers ( Those APIs are remote cache APIs, not static cache APIs. They continue to work because they:
✔️ Why your example still worksEven though the route is dynamic: await connection(); // OR await cookies()this block: 'use cache';
cacheTag(`product-price-${productId}`);
cacheLife({ expire: 50 });creates a runtime-level memoized cache entry, not a static one. So caching still works exactly as expected:
This is intended behavior. ✔️ What doesn’t work in dynamic contextThe docs refer specifically to THIS type of caching: ❌ static deduping of
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
https://nextjs.org/docs/app/api-reference/directives/use-cache-remote
Doc says:
When Next.js encounters certain APIs like connection(), cookies(), or headers(), the context becomes "dynamic". In a dynamic context:
Regular use cache stops working - it won't cache anything
but here is changed sample from documentation on simple page
and I see that use cache works fine end expires correctly.
await cookies()
instead of
await connection();
works too
What do I miss?
Beta Was this translation helpful? Give feedback.
All reactions