javascript / intermediate
Snippet
Fine-grained Cache Invalidation with revalidateTag
Next.js allows you to assign tags to data fetch requests. By using revalidateTag, you can purge specific cache entries on-demand rather than relying on global timers, which is critical for maintaining data fresh across different components.
snippet.js
1
2
3
4
5
6
import { revalidateTag } from 'next/cache';export async function updateProfile(id) {await db.users.update(id);revalidateTag('user-profile');}
nextjs
Breakdown
1
import { revalidateTag } from 'next/cache';
Imports the utility function to trigger manual cache purges in the App Router.
2
revalidateTag('user-profile');
Instructs Next.js to invalidate all cached data that was fetched with the 'user-profile' tag.