Logo di Cache Components

Cache Components

Community
laguagu
cache-components

Indicazioni di livello esperto su Cache Components e Partial Prerendering (PPR) di Next.js. Usala per implementare la direttiva 'use cache', configurare la durata della cache con cacheLife(), aggiungere tag ai dati memorizzati nella cache con cacheTag(), invalidare la cache con updateTag()/revalidateTag(), ottimizzare i confini tra contenuti statici e dinamici, gestire 'use cache: private' per scenari di conformità, applicare pattern di pass-through/interleaving, memorizzare nella cache le richieste GET degli Route Handler, eseguire il debug dei problemi di cache e revisionare le implementazioni di Cache Components.

Add to TypingMind

Panoramica

Pubblicato dalaguagu
Repositoryclaude-code-nextjs-skills
Nome della skillcache-components
Stelle
64
Fork
18
File inclusi
3
LicenzaMIT
Link
  • Istruzioni in Markdown

    Un file SKILL.md che il modello carica quando serve, consumando token solo quando una richiesta è effettivamente pertinente.

  • Funziona con qualsiasi LLM

    Le skill IA sono in semplice Markdown, non in codice specifico di un fornitore, quindi funzionano con GPT, Claude, Gemini, Grok o un modello locale.

  • File inclusi: 3

    Script, template e riferimenti che il modello può leggere mentre lavora. I file sono di sola lettura e non vengono mai eseguiti.

  • Open source

    Pubblicata da laguagu su GitHub. Leggi i sorgenti prima di installarla.

Installazione

Installa la skill IA Cache Components in TypingMind per usarla con qualsiasi LLM, oppure copiala in un altro agente in grado di leggere SKILL.md.

1

Installa in TypingMind

TypingMind installa una skill direttamente dalla sua cartella GitHub: legge SKILL.md, raccoglie i file delle risorse e salva il risultato in locale.

  1. Apri l'app e vai su Plugin → Skill.
  2. Scegli "Installa da GitHub".
  3. Incolla l'URL della cartella della skill riportato qui sotto e conferma.
    https://github.com/laguagu/claude-code-nextjs-skills/tree/main/skills/cache-components
  4. Attiva la skill in ogni chat in cui vuoi averla a disposizione.
Plugin → Skill → Aggiungi skill → Da URL GitHub, poi incolla l'URL della cartella e premi Continua.
2

Installa in un altro agente

Qualsiasi agente che legge il formato Agent Skills può usare questa skill: copia la cartella della skill nella directory delle skill di quell'agente.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/laguagu/claude-code-nextjs-skills.git /tmp/claude-code-nextjs-skills
mkdir -p .claude/skills
cp -r /tmp/claude-code-nextjs-skills/skills/cache-components .claude/skills/cache-components
Dopo la copia, riavvia Claude Code per caricare la nuova skill.

Usala in TypingMind

Attiva Cache Components in qualsiasi chat di TypingMind e al resto pensa il modello. Il nome e la descrizione della skill sono nel prompt di sistema e, non appena arriva una richiesta pertinente, il modello carica da solo le istruzioni complete: non devi mai richiamarla manualmente e non consuma token finché non viene effettivamente usata.

Il modello carica automaticamente Cache Components non appena riceve una richiesta pertinente.

Funziona con qualsiasi modello IA

Le skill IA sono semplici istruzioni in Markdown, non codice specifico di un fornitore, quindi Cache Components non è vincolata al modello per cui è stata scritta. Installala una sola volta in TypingMind e usala con GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama o un modello locale che esegui tu, sempre con le tue chiavi API.

  • Caricata solo quando serve

    Il prompt di sistema contiene solo il nome e la descrizione. Le istruzioni vengono recuperate alla prima richiesta pertinente, quindi una skill inutilizzata non costa nulla.

  • Cambia modello durante la chat

    Poiché la skill è composta da istruzioni e non da codice, continua a funzionare anche se cambi modello: il nuovo modello legge lo stesso file SKILL.md.

Istruzioni della skill

Questo è il contenuto di SKILL.md che il modello carica. Leggilo prima dell'installazione: una skill è un insieme di istruzioni che il tuo modello seguirà.

Next.js Cache Components

Auto-activation: Activate this skill automatically in Next.js projects that have cacheComponents: true in next.config.ts/next.config.js. When detected, apply Cache Components patterns to all Server Component authoring, data fetching, and caching decisions.

Project Detection

When starting work in a Next.js project, check if Cache Components are enabled:

bash
# Check next.config.ts or next.config.js for cacheComponents
grep -r "cacheComponents" next.config.* 2>/dev/null

If cacheComponents: true is found, apply this skill's patterns proactively when:

  • Writing React Server Components
  • Implementing data fetching
  • Creating Server Actions with mutations
  • Optimizing page performance
  • Reviewing existing component code

Cache Components enable Partial Prerendering (PPR) - mixing static HTML shells with dynamic streaming content for optimal performance. Cache Components also enable state preservation during navigation with React's <Activity> component, which can keep cached component trees mounted but hidden.

Philosophy: Code Over Configuration

Cache Components represents a shift from segment configuration to compositional code:

Before (Deprecated)After (Cache Components)
export const revalidate = 3600cacheLife('hours') inside 'use cache'
export const dynamic = 'force-static'Use 'use cache' and Suspense boundaries
All-or-nothing static/dynamicGranular: static shell + cached + dynamic

Key Principle: Components co-locate their caching, not just their data. Next.js provides build-time feedback to guide you toward optimal patterns.

Core Concept

┌─────────────────────────────────────────────────────┐
│                   Static Shell                       │
│  (Sent immediately to browser)                       │
│                                                      │
│  ┌─────────────┐  ┌─────────────┐  ┌─────────────┐  │
│  │   Header    │  │  Cached     │  │  Suspense   │  │
│  │  (static)   │  │  Content    │  │  Fallback   │  │
│  └─────────────┘  └─────────────┘  └──────┬──────┘  │
│                                           │         │
│                                    ┌──────▼──────┐  │
│                                    │  Dynamic    │  │
│                                    │  (streams)  │  │
│                                    └─────────────┘  │
└─────────────────────────────────────────────────────┘

Mental Model: The Caching Decision Steps

When writing a React Server Component, walk through these steps in order:

  1. Does the component fetch data or perform I/O?

    • No → pure component, nothing to decide.
    • Yes → continue.
  2. Does it depend on request context (cookies(), headers(), searchParams)?

    • No → continue to step 3.
    • Yes → continue to step 4.
  3. (No request context) Is the data the same across users?

    • Yes → add 'use cache' with cacheTag() and cacheLife().
    • No → wrap rendering in <Suspense> so the dynamic part streams at request time.
  4. (Has request context) Can you extract the runtime data as function arguments?

    • Yes → read cookies()/headers() outside the cached scope, pass values into a 'use cache' function, and wrap the dynamic caller in <Suspense>.
    • No (compliance prevents cross-request sharing) → use 'use cache: private' (experimental — not recommended for production) as a last resort, still wrapped in <Suspense>.

Key insight: 'use cache' is for data that is the same across users. User-specific data stays dynamic and streams through <Suspense>. Reach for 'use cache: private' only when you cannot refactor runtime data into arguments.

Quick Start

Enable Cache Components

typescript
// next.config.ts
import type { NextConfig } from 'next'

const nextConfig: NextConfig = {
  cacheComponents: true,
}

export default nextConfig

Basic Usage

tsx
// Cached component - output included in static shell
async function CachedPosts() {
  'use cache'
  const posts = await db.posts.findMany()
  return <PostList posts={posts} />
}

// Page with static + cached + dynamic content
export default async function BlogPage() {
  return (
    <>
      <Header /> {/* Static */}
      <CachedPosts /> {/* Cached */}
      <Suspense fallback={<Skeleton />}>
        <DynamicComments /> {/* Dynamic - streams */}
      </Suspense>
    </>
  )
}

Server Actions vs Data Fetching (Critical Rule)

Server Actions are for MUTATIONS ONLY - never for data fetching:

PurposeUseExample Functions
Data fetchServer Component / 'use cache'getProducts(), getUser(id)
MutationServer Action ('use server')createProduct(), updateUser(), deletePost()

❌ WRONG: Server Action for Data Fetching

tsx
"use server"
export async function getProducts() {
  return await db.products.findMany() // NO! This is not a mutation
}

"use server"
export async function getTheme() {
  return (await cookies()).get("theme")?.value // NO! Just reading data
}

✅ CORRECT: Data Function + Server Component

tsx
// data/products.ts - Cached data function
export async function getProducts() {
  "use cache"
  cacheTag("products")
  cacheLife("hours")
  return await db.products.findMany()
}

// page.tsx - Server Component reads data directly
import { cookies } from "next/headers"

export default async function Page() {
  const products = await getProducts()
  const theme = (await cookies()).get("theme")?.value ?? "light"
  return <ProductList products={products} theme={theme} />
}

✅ CORRECT: Server Action for Mutation

tsx
"use server"
import { updateTag } from "next/cache"

export async function createProduct(formData: FormData) {
  await db.products.create({ data: formData })
  updateTag("products") // Invalidate cache after mutation
}

Core APIs

1. 'use cache' Directive

Marks code as cacheable. Can be applied at three levels:

tsx
// File-level: All exports are cached
'use cache'
export async function getData() {
  /* ... */
}
export async function Component() {
  /* ... */
}

// Component-level
async function UserCard({ id }: { id: string }) {
  'use cache'
  const user = await fetchUser(id)
  return <Card>{user.name}</Card>
}

// Function-level
async function fetchWithCache(url: string) {
  'use cache'
  return fetch(url).then((r) => r.json())
}

Important: All cached functions must be async.

2. cacheLife() - Control Cache Duration

tsx
import { cacheLife } from 'next/cache'

async function Posts() {
  'use cache'
  cacheLife('hours') // Use a predefined profile

  // Or custom configuration:
  cacheLife({
    stale: 60, // 1 min - client cache validity
    revalidate: 3600, // 1 hr - start background refresh
    expire: 86400, // 1 day - absolute expiration
  })

  return await db.posts.findMany()
}

Predefined profiles: 'default', 'seconds', 'minutes', 'hours', 'days', 'weeks', 'max'

3. cacheTag() - Tag for Invalidation

tsx
import { cacheTag } from 'next/cache'

async function BlogPosts() {
  'use cache'
  cacheTag('posts')
  cacheLife('days')

  return await db.posts.findMany()
}

async function UserProfile({ userId }: { userId: string }) {
  'use cache'
  cacheTag('users', `user-${userId}`) // Multiple tags

  return await db.users.findUnique({ where: { id: userId } })
}

4. updateTag() - Immediate Invalidation

For read-your-own-writes semantics. updateTag() is only callable from a Server Action and throws in Route Handlers and other contexts; use revalidateTag(tag, 'max') outside a Server Action:

tsx
'use server'
import { updateTag } from 'next/cache'

export async function createPost(formData: FormData) {
  await db.posts.create({ data: formData })

  updateTag('posts') // Client immediately sees fresh data
}

5. revalidateTag() - Background Revalidation

For stale-while-revalidate pattern:

tsx
'use server'
import { revalidateTag } from 'next/cache'

export async function updatePost(id: string, data: FormData) {
  await db.posts.update({ where: { id }, data })

  revalidateTag('posts', 'max') // Serve stale, refresh in background
}

⚠️ Deprecated: The single-argument form revalidateTag('posts') is deprecated. Always pass a profile ('max' is recommended for stale-while-revalidate) or { expire: <seconds> } as the second argument. For webhooks that require immediate expiration, use revalidateTag(tag, { expire: 0 }). For immediate read-your-own-writes in Server Actions, prefer updateTag() instead.

When to Use Each Pattern

Content TypeAPIBehavior
StaticNo directiveRendered at build time
Cached'use cache'Included in static shell, revalidates
DynamicInside <Suspense>Streams at request time

Parameter Permutations & Subshells

Critical Concept: With Cache Components, Next.js renders ALL permutations of provided parameters to create reusable subshells.

tsx
// app/products/[category]/[slug]/page.tsx
export async function generateStaticParams() {
  return [
    { category: 'jackets', slug: 'classic-bomber' },
    { category: 'jackets', slug: 'essential-windbreaker' },
    { category: 'accessories', slug: 'thermal-fleece-gloves' },
  ]
}

Next.js renders these routes:

/products/jackets/classic-bomber        ← Full params (complete page)
/products/jackets/essential-windbreaker ← Full params (complete page)
/products/accessories/thermal-fleece-gloves ← Full params (complete page)
/products/jackets/[slug]                ← Partial params (category subshell)
/products/accessories/[slug]            ← Partial params (category subshell)
/products/[category]/[slug]             ← No params (fallback shell)

Why this matters: The category subshell (/products/jackets/[slug]) can be reused for ANY jacket product, even ones not in generateStaticParams. Users navigating to an unlisted jacket get the cached category shell immediately, with product details streaming in.

generateStaticParams Requirements

With Cache Components enabled:

  1. Must provide at least one parameter - Empty arrays now cause build errors (prevents silent production failures)
  2. Params prove static safety - Providing params lets Next.js verify no dynamic APIs are called
  3. Partial params create subshells - Each unique permutation generates a reusable shell
tsx
// ❌ ERROR with Cache Components
export function generateStaticParams() {
  return [] // Build error: must provide at least one param
}

// ✅ CORRECT: Provide real params
export async function generateStaticParams() {
  const products = await getPopularProducts()
  return products.map(({ category, slug }) => ({ category, slug }))
}

Cache Key = Arguments

Arguments become part of the cache key:

tsx
// Different userId = different cache entry
async function UserData({ userId }: { userId: string }) {
  'use cache'
  cacheTag(`user-${userId}`)

  return await fetchUser(userId)
}

Build-Time Feedback

Cache Components provides early feedback during development. These build errors guide you toward optimal patterns:

Error: Dynamic data outside Suspense

Error: Accessing cookies/headers/searchParams outside a Suspense boundary

Solution: Wrap dynamic components in <Suspense>:

tsx
<Suspense fallback={<Skeleton />}>
  <ComponentThatUsesCookies />
</Suspense>

Error: Uncached data outside Suspense

Error: Accessing uncached data outside Suspense

Solution: Either cache the data or wrap in Suspense:

tsx
// Option 1: Cache it
async function ProductData({ id }: { id: string }) {
  'use cache'
  return await db.products.findUnique({ where: { id } })
}

// Option 2: Make it dynamic with Suspense
;<Suspense fallback={<Loading />}>
  <DynamicProductData id={id} />
</Suspense>

Error: Request data inside cache

Error: Cannot access cookies/headers inside 'use cache'

Solution: Extract runtime data outside cache boundary (see "Handling Runtime Data" above).

Additional Resources

Code Generation Guidelines

When generating Cache Component code:

  1. Always use async - All cached functions must be async
  2. Place 'use cache' first - Must be first statement in function body
  3. Call cacheLife() early - Should follow 'use cache' directive
  4. Tag meaningfully - Use semantic tags that match your invalidation needs
  5. Extract runtime data - Move cookies()/headers() outside cached scope
  6. Wrap dynamic content - Use <Suspense> for non-cached async components
  7. Use 'use cache: private' as last resort - Experimental (not recommended for production). Only when runtime data cannot be extracted as params AND compliance requires no cross-request sharing

Review Checklist

When reviewing code in Cache Components projects, flag these issues:

  • Data fetching without 'use cache' where caching would benefit
  • Missing cacheTag() calls (makes invalidation impossible)
  • Missing cacheLife() (relies on defaults which may not be appropriate)
  • Server Actions without updateTag()/revalidateTag() after mutations
  • updateTag() outside a Server Action - use revalidateTag(tag, 'max')
  • cookies()/headers() called inside 'use cache' scope
  • Dynamic components without <Suspense> boundaries
  • DEPRECATED: export const revalidate - replace with cacheLife() in 'use cache'
  • DEPRECATED: export const dynamic - replace with Suspense + cache boundaries
  • Empty generateStaticParams() return - must provide at least one param
  • Single-argument revalidateTag('tag') - use two-argument form with profile or { expire }

File inclusi

Il modello legge questi file quando servono, mentre la skill è caricata. Sono accessibili in sola lettura e non vengono mai eseguiti.

Domande frequenti

Cosa fa la skill IA Cache Components?

Indicazioni di livello esperto su Cache Components e Partial Prerendering (PPR) di Next.js. Usala per implementare la direttiva 'use cache', configurare la durata della cache con cacheLife(), aggiungere tag ai dati memorizzati nella cache con cacheTag(), invalidare la cache con updateTag()/revalidateTag(), ottimizzare i confini tra contenuti statici e dinamici, gestire 'use cache: private' per scenari di conformità, applicare pattern di pass-through/interleaving, memorizzare nella cache le richieste GET degli Route Handler, eseguire il debug dei problemi di cache e revisionare le implementa...

Perché usare Cache Components su TypingMind?

Perché la installi una volta e la usi con qualsiasi modello. Cache Components è scritta in semplice Markdown anziché in codice specifico di un fornitore, quindi la stessa skill funziona con GPT-5, Claude, Gemini, Grok o un modello locale, e puoi cambiare modello durante la chat senza che la skill smetta di funzionare. TypingMind usa le tue chiavi API, quindi paghi direttamente i fornitori anziché un abbonamento per posto utente, e le tue skill e chat restano nel tuo spazio di archiviazione.

Come installo Cache Components in TypingMind?

Apri Plugin → Skill → Installa da GitHub in TypingMind e incolla https://github.com/laguagu/claude-code-nextjs-skills/tree/main/skills/cache-components. TypingMind legge il file SKILL.md, include i file associati e installa la skill, che puoi abilitare per ogni singola chat.

Quali modelli IA possono usare Cache Components?

Qualsiasi modello che colleghi a TypingMind. Le skill IA sono semplici istruzioni in Markdown anziché codice specifico di un fornitore, quindi GPT, Claude, Gemini, Grok e i modelli locali possono tutti caricare questa skill quando una richiesta corrisponde al suo scopo.

Quanti modelli IA posso usare con Cache Components?

Quanti ne vuoi. Se un modello supporta le skill, puoi usarlo con Cache Components: GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama e molti altri, tutti su TypingMind con le tue chiavi API.

La skill IA Cache Components è gratuita?

Sì. È pubblicata su GitHub da laguagu con licenza MIT. Paghi solo il tuo fornitore IA per i token che usi.

Cosa sono le skill IA?

Una skill IA è un pacchetto di istruzioni riutilizzabili che insegna a un modello IA come svolgere un'attività specifica. Segue il formato aperto Agent Skills: un file SKILL.md con un nome e una descrizione, più eventuali script, template o file di riferimento di cui il modello potrebbe aver bisogno. Il modello legge le istruzioni solo quando la tua richiesta corrisponde alla skill, quindi una skill installata non comporta costi finché non viene usata.

In cosa si differenziano le skill IA dai plugin o dai server MCP?

Un plugin o un server MCP fornisce a un modello nuovi strumenti da richiamare: codice che viene eseguito da qualche parte e restituisce un risultato. Una skill IA, invece, fornisce al modello conoscenze e procedure: come affrontare un'attività, quali passaggi seguire e quali caratteristiche deve avere un buon output. Le skill sono semplici istruzioni in Markdown, quindi non richiedono server, chiavi API né ambienti di esecuzione e funzionano con qualsiasi modello.

Elenco completo

Crea subito il tuo spazio di lavoro AI

Iscriviti alla nostra newsletter per ricevere aggiornamenti sulle nuove funzionalità e sulle prossime iniziative a premi 👇