---
name: "nextjs-revalidate-tag-on-demand"
description: "Invalidate cached fetch requests and database queries instantly using targeted cache tags."
metadata:
  author: "Vercel"
  version: "1.0.0"
  runtime_compatibility:
    - "codex"
    - "claude-code"
    - "cursor"
    - "windsurf"
triggers:
  - "purge cache nextjs revalidatetag"
  - "tag fetch requests nextjs"
  - "unstable_cache nextjs"
permissions:
  fs_read: true
  fs_write: "workspace"
  shell_exec: false
  net_access: false
---

# Next.js On-Demand Cache Invalidation via revalidateTag

Implements granular caching with Next.js 15: configuring fetch with next: { tags: ['posts'] }, stable cache keys with unstable_cache, triggering webhook invalidations via revalidateTag, and stale-while-revalidate.

## When to Invoke
Invoke this skill whenever you need to:
- purge cache nextjs revalidatetag
- tag fetch requests nextjs
- unstable_cache nextjs

## Execution SOP & Rules
1. Attach tags to fetch calls: fetch(url, { next: { tags: ['products', `product-${id}`] } }).
2. Wrap raw DB queries: const getProduct = unstable_cache(fn, ['product-key'], { tags: ['products'] }).
3. Trigger purge in Server Action or Route Handler: revalidateTag('products').
4. Verify subsequent requests fetch fresh data without requiring full site rebuilds.

## Verification Checklist
- Run local linting, syntax validation, and type-checks before reporting completion.
- Ensure all created or modified files adhere to workspace boundaries and avoid uncommitted secrets.
- Verify tests or build commands exit cleanly with return code 0.
