updateTag
API Reference for the updateTag function.
updateTag
allows you to update cached data on-demand for a specific cache tag from within Server Actions.
This function is designed for read-your-own-writes scenarios, where a user makes a change (like creating a post), and the UI immediately shows the change, rather than stale data.
Usage
updateTag
can only be called from within Server Actions. It cannot be used in Route Handlers, Client Components, or any other context.
If you need to invalidate cache tags in Route Handlers or other contexts, use revalidateTag
instead.
Good to know: updateTag
immediately expires the cached data for the specified tag. The next request will wait to fetch fresh data rather than serving stale content from the cache, ensuring users see their changes immediately.
Parameters
tag
: A string representing the cache tag associated with the data you want to update. Must not exceed 256 characters. This value is case-sensitive.
Tags must first be assigned to cached data. You can do this in two ways:
- Using the
next.tags
option withfetch
for caching external API requests:
- Using
cacheTag
inside cached functions or components with the'use cache'
directive:
Returns
updateTag
does not return a value.
Differences from revalidateTag
While both updateTag
and revalidateTag
invalidate cached data, they serve different purposes:
-
updateTag
:- Can only be used in Server Actions
- Next request waits for fresh data (no stale content served)
- Designed for read-your-own-writes scenarios
-
revalidateTag
:- Can be used in Server Actions and Route Handlers
- With
profile="max"
(recommended): Serves cached data while fetching fresh data in the background (stale-while-revalidate) - With custom profile: Can be configured to any cache life profile for advanced usage
- Without profile: legacy behavior which is equivalent to
updateTag
Examples
Server Action with Read-Your-Own-Writes
Error when used outside Server Actions
When to use updateTag
Use updateTag
when:
- You're in a Server Action
- You need immediate cache invalidation for read-your-own-writes
- You want to ensure the next request sees updated data
Use revalidateTag
instead when:
- You're in a Route Handler or other non-action context
- You want stale-while-revalidate semantics
- You're building a webhook or API endpoint for cache invalidation
Related
revalidateTag
- For invalidating tags in Route HandlersrevalidatePath
- For invalidating specific paths