cacheTag
Next.JS Use Cache
2 min read
Published Jul 3 2025
Guide Sections
Guide Comments
In order to clear a specific cache, we first need to tag the data result, then we can call the revalidateTag
function to clear any data linked to that tag so that a fresh request to the database will be made next time it is requested.
To tag the function, we use the cacheTag
function. We provide this function with a list of 1 or more string values. Any of these string values can be used to revalidate the cache. Eg. in our getCategory function, we could choose to add two tags. One for categories
, to clear all category data and one for category:xx
, with xx being the category id. This then enables us to clear the cache for every category at once, or the cache for a specific category id. This then enables us to add a revalidateTag
function in the update a specific category function to only clear the cache of that category, but also allows us to have a much wider reaching clear all the categories caches admin function for emergencies etc. The tag used can be any single string, but i personally like to use the :
to break up the data as i know i wont have a :
in the id fields. You could just as easily use a -
or a |
etc. or have no seperator at all and just have categoryxx
.
So to tag the function in our example in the last section we would do:
This then adds two tags to the function. One for categories
and one for category:id
.
Then you would call this to clear the cache for all the categories:
And in your update category function you could call something like:
So now we can cache the data and clear the cache, next is how to choose how long the cache gets saved for in the next section.