unstable_cache tags based on the request result
Unanswered
Paper wasp posted this in #help-forum
Paper waspOP
Hi !
I'm using contentful and Next.js, with the ISR feature. I would like to tag the cache with the different IDs from the contentful entries, so that I can invalidate them easily with the Contentful webhook.
I cannot find a proper way to tag the content with unstable_cache based on the request result. Basically what I want to achieve is what is done here https://nextjs.org/docs/app/api-reference/functions/cacheTag#creating-tags-from-external-data with 'use cache' but with unstable_cache since 'use cache' and cacheTags are not available on the stable release.
Any idea ?
so far the only way I found is to fetch before and leave the async function empty in the unstable_cache but this only helps to tag the page and looses the cache when fetching the RSC.
I'm using contentful and Next.js, with the ISR feature. I would like to tag the cache with the different IDs from the contentful entries, so that I can invalidate them easily with the Contentful webhook.
I cannot find a proper way to tag the content with unstable_cache based on the request result. Basically what I want to achieve is what is done here https://nextjs.org/docs/app/api-reference/functions/cacheTag#creating-tags-from-external-data with 'use cache' but with unstable_cache since 'use cache' and cacheTags are not available on the stable release.
Any idea ?
so far the only way I found is to fetch before and leave the async function empty in the unstable_cache but this only helps to tag the page and looses the cache when fetching the RSC.
2 Replies
Brown bear
I strongly suspect it's not possible, and that's why they've added
1. Aggressively revalidate anywhere that could be affected when a record is updated. This might be worth it if the record is actually unlikely to be updated very often …
2. Make all queries flat (depth: 0). E.g. query the page content, then find all the ImageUpload references within, and send a query that gets all of those. I actually did this for another project and it wasn't too bad. It meant revalidation was highly targeted and quick, and every query sent was fast to execute.
3. (I think I might do this until
unstable_cacheTag
. I have the same requirements (albeit with Payload CMS and not Contentful, so the terminology I use might not be quite right). Options as I see it:1. Aggressively revalidate anywhere that could be affected when a record is updated. This might be worth it if the record is actually unlikely to be updated very often …
2. Make all queries flat (depth: 0). E.g. query the page content, then find all the ImageUpload references within, and send a query that gets all of those. I actually did this for another project and it wasn't too bad. It meant revalidation was highly targeted and quick, and every query sent was fast to execute.
3. (I think I might do this until
unstable_cacheTag
is available) const cachedQuery = (additionalTags: string[]) => unstable_cache(queryFn, [], { ['base-tag', ...additionalTags ] })
. Run cachedQuery([])
, inspect the result for the additional tags, and run cachedQuery(['additional', 'tags'])
and use the response from that instead.P.S. I have tested 3 out and it does work as expected