#use cache

1 messages · Page 1 of 1 (latest)

hardy jolt
#

I'm struggling with understanding how next'js caches data when using the use cache directive.
My main question is does nextjs cache data globally for all users to use.
Reading docs it says it generates the cache key by using the function id and the functions args. Does nextjs cache this data globally using the generated key? If it does my concern would then be around security if fetching say a users id inside the function instead of passing it through args and using it to fetch data and cache it would this then not be shown to everyone calling that same function which would then hit the cache?

How does revalidation then work? say i use unstable_cacheTag("known_tag") and i then use revalidateTag does that clear the cache for all users globally? there can be many different cached functions with the same cache tag so im just wondering if revalidating that tag calls all those functions to be revalidated

torpid wyvernBOT
#

Hi @hardy jolt. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

I'll help clarify how Next.js caching works with the use cache directive and address your security and revalidation concerns.

Global Caching Behavior

Yes, Next.js does cache data globally across all users when using the use cache directive. The cache is shared among all users of your application, which is an important consideration for security.

According to the documentation, the cache key is generated using:

  • Build ID (unique to each deployment)
  • Function ID (unique identifier for the function)
  • Serializable function arguments/props

As mentioned in the docs, "the same cache entry will be reused as long as its inputs are the same" Next.js use cache documentation.

Security Implications

Your concern about security is valid. If you're fetching user-specific data inside a cached function without passing the user ID as an argument, that data could potentially be exposed to other users.

For proper security:

  • Always pass user-specific identifiers as arguments to cached functions
  • This ensures different cache entries for different users
  • Non-serializable values (like complex objects) won't become part of the cache key

How Revalidation Works

When you use revalidateTag, it does indeed clear the cache globally for all users. According to the Vercel documentation:

"When a revalidation is triggered, the path or cache tag is marked stale globally in every Vercel Edge Network region. The next request to that path or tag, in any region, will trigger a revalidation and update the cache globally. The regional cache in all regions is purged and updated within 300ms." Vercel Data Cache documentation

#

When you call revalidateTag with a specific tag, it will invalidate all cache entries associated with that tag. This means:

  • All functions/components tagged with that specific tag will be revalidated
  • The revalidation happens globally across all users
  • The next request will trigger a fresh fetch for the data

Cache Tag Implementation

You can tag your cached data using cacheTag:

import { unstable_cacheTag as cacheTag } from 'next/cache'

export async function getData() {
  'use cache'
  cacheTag('my-data')
  const data = await fetch('/api/data')
  return data
}

Then revalidate it using:

'use server'

import { revalidateTag } from 'next/cache'

export default async function submit() {
  await updateData()
  revalidateTag('my-data')
}

This will invalidate all cache entries tagged with 'my-data' Next.js cacheTag documentation.

Remember that for user-specific data, you should always pass the user identifier as an argument to ensure proper cache isolation between users.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: