What is the purpose of keyParts in unstable_cache since it cannot be revalidated ?
Unanswered
Scottish Fold posted this in #help-forum
Scottish FoldOP
Example:
The first intuition I had about this key was that I could revalidate this with revalidateTags("user-43")
However that doesn't work, we must use
Is not clear why we must be repetitive
Current docs is not clear too
https://nextjs.org/docs/app/api-reference/functions/unstable_cache
const getCachedUser = unstable_cache(fetchUser, [`user-43`])
The first intuition I had about this key was that I could revalidate this with revalidateTags("user-43")
However that doesn't work, we must use
const getCachedUser = unstable_cache(fetchUser, [`user-43`], {
revalidate: false,
tags: ["user-43"]
})
Is not clear why we must be repetitive
Current docs is not clear too
https://nextjs.org/docs/app/api-reference/functions/unstable_cache
34 Replies
Scottish FoldOP
up
@Anay-208 | Ping in replies You’ve to add a tag so you can re validate it
Scottish FoldOP
yes, so what is the purpose of the keyParts if it cannot be revalidated and we need to use tags anyway
@Scottish Fold yes, so what is the purpose of the keyParts if it cannot be revalidated and we need to use tags anyway
It’s actually for nextjs to identify if it’s a unique item
And tag is for revalidating
Scottish FoldOP
yeah, it's written in the docs, but it's not clear what it means. For example, I see it can be undefined, what is the difference ? it's not clear
About that, I’m also not sure.
All in know is that:
The 2nd argument is for next js to identify. If you set it to undefined, then only 1 can be there
All in know is that:
The 2nd argument is for next js to identify. If you set it to undefined, then only 1 can be there
You’re right docs don’t clearly state about it. But as far as I understood, I’ve told
Scottish FoldOP
I haven't any error using undefined in keyParts, at least with tags
It's basically how nextjs relates their cache to it
Yes it can be undefined as well.. it's just a way to uniquely identify it
Yes it can be undefined as well.. it's just a way to uniquely identify it
It won't cause any syntax errors, your just gonna see your cache not hitting or revalidating if they depend on unique values
Scottish FoldOP
well, revalidating won't works anyway
only tags can be revalidated
That’s right
Or path can also be
Scottish FoldOP
keyParts seems to be a useless and counterintuitive param
Not really
Take this as an example
Imagine you have an article
And you need to fetch related articles for it
Which are linked to the article url
Now unless you put the article url in the keyparams
Nextjs won't uniquely cache them
It's just gonna be one cache otherwise
So even after you change the url, unstable cache won't fetch the new related articles
Scottish FoldOP
yeah, but caching without control is dangerous, we need always to be able to revalidate it. In that case tags and revalidate param is preferable
and keyParts with tags is repetitive, so why both ?
Scottish FoldOP
why tags can't be used to identify ?
fetch cache works well without keyParts
Western thatching ant
But the
cb
args are added to the cache key, so as long as the args uniquely identify the value, keyParts
is unnecessary: https://github.com/vercel/next.js/blob/e078fd1715254c5c64013b79dbeefcb3a42bc9ac/packages/next/src/server/web/spec-extension/unstable-cache.ts#L111