Next.js Discord

Discord Forum

What is the purpose of keyParts in unstable_cache since it cannot be revalidated ?

Unanswered
Scottish Fold posted this in #help-forum
Open in Discord
Avatar
Scottish FoldOP
Example:
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

33 Replies

Avatar
Scottish FoldOP
up
Avatar
() => "anay-208"
You’ve to add a tag so you can re validate it
Avatar
Scottish FoldOP
yes, so what is the purpose of the keyParts if it cannot be revalidated and we need to use tags anyway
Avatar
() => "anay-208"
It’s actually for nextjs to identify if it’s a unique item
And tag is for revalidating
Avatar
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
Avatar
() => "anay-208"
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
You’re right docs don’t clearly state about it. But as far as I understood, I’ve told
Avatar
Scottish FoldOP
I haven't any error using undefined in keyParts, at least with tags
Avatar
Arinji
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
It won't cause any syntax errors, your just gonna see your cache not hitting or revalidating if they depend on unique values
Avatar
Scottish FoldOP
well, revalidating won't works anyway
only tags can be revalidated
Avatar
() => "anay-208"
Yup
That’s right
Or path can also be
Avatar
Scottish FoldOP
keyParts seems to be a useless and counterintuitive param
Avatar
Arinji
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
Avatar
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 ?
Avatar
Arinji
They aren't the same
Tags are to revalidate
Keys are to identify
Avatar
Scottish FoldOP
why tags can't be used to identify ?
fetch cache works well without keyParts