Agent-skills-standard nextjs-caching
Configure the 4 caching layers in Next.js: request memoization, data cache, full-route cache, and router cache. Use when setting revalidation strategies, invalidating cached data with tags, or diagnosing stale data bugs. (triggers: **/page.tsx, **/layout.tsx, **/action.ts, unstable_cache, revalidateTag, Router Cache, Data Cache)
install
source · Clone the upstream repo
git clone https://github.com/HoangNguyen0403/agent-skills-standard
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/HoangNguyen0403/agent-skills-standard "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/nextjs/nextjs-caching" ~/.claude/skills/hoangnguyen0403-agent-skills-standard-nextjs-caching && rm -rf "$T"
manifest:
skills/nextjs/nextjs-caching/SKILL.mdsource content
Caching Architecture
Priority: P1 (HIGH)
Next.js 4 distinct caching layers. Understanding them prevents stale data bugs.
Workflow: Configure Caching for Feature
- Choose cache strategy — SSG (
), ISR (force-cache
), or SSR (revalidate: N
).no-store - Tag cacheable fetches — Add
to fetch options.next: { tags: ['posts'] } - Invalidate on mutation — Call
in Server Actions.revalidateTag('posts') - Deduplicate requests — Wrap shared data fetches with React
.cache() - Clear client cache — Use
after client-side mutations.router.refresh()
Cache Invalidation Example
Implementation Guidelines
- Next.js 15+ Standard: Use
withfetch
orrevalidate: number
for API calls. Usecache: 'force-cache'
or newunstable_cache
(experimental) for custom data stores.'use cache' - Layers: Distinguish between Data Cache (persistent across requests) and Request Memoization (React's lifecycle specific). Use
from React to deduplicate fetches within single render.cache() - Invalidation: Use
after mutations orrevalidatePath('/')
for granular cache purging.revalidateTag('tag-name') - Client Cache: Understand Router Cache (in-memory on client) and its 30s-min lifespan. Clear it using
.router.refresh() - Static Assets: Leverage
for pre-rendering static routes at build time. Use ISR (Incremental Static Regeneration) for content that updates periodically.generateStaticParams - Streaming: Combine
withSuspense
triggers to prevent slow data from blocking entire page render.fetch - Next.js 16+: Favor
and'use cache'
overcacheLife()
where available for deterministic caching.revalidate: number
| Layer | Where | Control |
|---|---|---|
| Request Memoization | Server | React |
| Data Cache | Server | , |
| Full Route Cache | Server | Static Prerendering |
| Router Cache | Client | |
Implementation Details
See Cache Components & PPR for detailed key generation, closure constraints, and invalidation strategies.
Anti-Patterns
- No
in Next.js 16+: Useunstable_cache
directive with'use cache'
instead.cacheLife() - No
for server data: Preferrouter.refresh()
for targeted cache busting.revalidateTag() - No caching user-specific data at route level: Wrap personal data in
with<Suspense>
.'use cache' - No long-lived cache without tags: Assign
for fine-grained invalidation control.cacheTag()