#unstable_cache tags based on the request result

1 messages · Page 1 of 1 (latest)

dry tiger
#

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.

Learn how to use the cacheTag function to manage cache invalidation in your Next.js application.

rocky idolBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

barren girder
#

I strongly suspect it's not possible, and that's why they've added 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.