What is the purpose of keyParts in unstable_cache since it cannot be revalidated ? #63099
Replies: 5 comments
-
I agree, I faced the same problem for a while. They do acknowledge this in the youtube video on caching in nextjs. However, the docs are not clear about this. |
Beta Was this translation helpful? Give feedback.
-
I think cache without control is very dangerous. It seems keyParts is a very counterintuitive and useless param. It can degrade dev experience, perhaps introducing some re-validation bugs because of this confusions. |
Beta Was this translation helpful? Give feedback.
-
If keyParts is used to identify, why not use tags instead ? Fetch cache works well without keyParts |
Beta Was this translation helpful? Give feedback.
-
yes also don't really get why we need both keyparts and tags 🤔 |
Beta Was this translation helpful? Give feedback.
-
Tags and cache keys are orthogonal concepts. The key maps one-to-one to a cache entry. The tags don't have to. One tag can map to multiple cache entries (and so you can revalidate multiple cache entries with one call to For example, say you run a website that has a shop and a blog. There are two api's you talk to: shopify and wordpress. Calls to the shop could look like: function getProductById(id) {
return unstable_cache(() => shopify.getProduct(id), ["shopify", id], {
tags: ["shopify", `shopify:${id}`],
})
} whereas the calls to wordpress could look like: function getBlogPostById(id) {
return unstable_cache(() => wordpress.getPost(id), ["wordpress", id], {
tags: ["wordpress" , "wordpress:${id}"],
})
} Now you add a global sale to your shop. You need to revalidate all products so the price can be updated. You can call This will:
|
Beta Was this translation helpful? Give feedback.
-
Summary
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
Beta Was this translation helpful? Give feedback.
All reactions