Ability to exclude specific parameters from cache key generation with use cache
and dynamicIO
in next.js 15
#79353
PouryaNoufallah96
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Goals
use cache
directive while adding more controlMy specific use case:
I'm using Clerk for authentication and have an external backend service that requires a token in API calls. Since I can't use Clerk's
getToken()
inside a method marked withuse cache
, I need to:Problem: Even when the token isn't expired, Clerk's
getToken()
returns a new token string each time it's called. This means my cache key changes with every request, effectively breaking caching altogether.Non-Goals
Background
The new caching model in Next.js 15 with
dynamicIO
anduse cache
is a significant improvement for simplifying data fetching and caching. However, it lacks granular control over the parameters affecting the cache key.For example, right now, I have a wrapper around my API call method to get the token and user from the clerk:
Then this is the place I'm using it:
And this is the API call file:
And readData is actually a helper method that is using the fetch under the hood
Proposal
I propose adding a way to specify which parameters should be excluded from the cache key generation when using the
use cache
directive. This could be implemented in several ways:"use cache { exclude: ['token'] }"
"use cache-exclude token"
"use cache" // @exclude-from-cache token
I'm interested in contributing to discussions around this feature and potentially helping with implementation if guidance is provided on the preferred approach.
Feature request:
Is there a way to specify that certain parameters (like an authentication token) should be ignored when generating the cache key? This would allow caching methods based only on relevant parameters while still accepting necessary values like authentication tokens.
Something like:
This would be extremely helpful for authentication scenarios where tokens need to be passed but shouldn't invalidate caches.
Beta Was this translation helpful? Give feedback.
All reactions