nextjs-data-caching
/nextjs-data-caching
Dobiera i stosuje podejście do pobierania i buforowania danych w Next.js 16: Cache Components, starszy model fetch, streaming Suspense i rewalidację.
- Autor
- autohandai ↗
- Repozytorium
- skillpack-nextjs-app-router ↗
- Licencja
- MIT
- Ostatnia zmiana repozytorium
- 19.10.2026
Ocena i bezpieczeństwo
- Pliki w paczce
- 1
- Skrypty
- 0
- Komentarze HTML
- 0
- Adresy URL w SKILL.md
- 1
Brak sygnałów. Repozytorium zmienione 19.10.2026, bez odwołań do wycofanych modeli i API.
MIT, otwarta: można używać i zmieniać, z zachowaniem informacji o autorze.
Co robi i kiedy
Dobiera i stosuje podejście do pobierania i buforowania danych w Next.js 16: Cache Components, starszy model fetch, streaming Suspense i rewalidację.
Wyzwalacze z opisu
Oryginalny opis (EN)
Chooses and applies the right data fetching and caching approach in a Next.js 16 App Router project, covering Cache Components ('use cache', cacheLife, cacheTag), the earlier fetch and segment config model, Suspense streaming, and revalidation. Use when data is stale, a page is unexpectedly dynamic or static, a build fails on caching config, or when adding a new data source to a page.
Jak działa
- Step 1: detect the model
- Section A: Cache Components
- Section B: the earlier model (no cacheComponents flag)
- Invalidation (both models)
- Fetching patterns
- Diagnosing stale or surprising data
- Checks before you finish
Podgląd SKILL.md
---
name: nextjs-data-caching
description: Chooses and applies the right data fetching and caching approach in a Next.js 16 App Router project, covering Cache Components ('use cache', cacheLife, cacheTag), the earlier fetch and segment config model, Suspense streaming, and revalidation. Use when data is stale, a page is unexpectedly dynamic or static, a build fails on caching config, or when adding a new data source to a page.
license: MIT
---
# Next.js data fetching and caching
Next.js 16 has two caching models, and the right code depends on which one the project uses. Find out first.
## Step 1: detect the model
Open `next.config.ts` (or `.js`/`.mjs`).
- `cacheComponents: true` means the project uses Cache Components. Follow section A.
- No flag means the project uses the earlier model. Follow section B.
Don't mix the two. With `cacheComponents` on, route segments that export `dynamic`, `revalidate`, `fetchCache`, `dynamicParams` or `runtime` fail the build.
## Section A: Cache Components
Data is dynamic by default. You opt in to caching with `'use cache'`.
```tsx
// lib/products.ts
import { cacheLife, cacheTag } from 'next/cache'
import { db } from '@/lib/db'
export async function getProducts() {
'use cache'
cacheLife('hours')
cacheTag('products')
return db.product.findMany()
}
```
```tsx
// app/shop/page.tsx
import { Suspense } from 'react'
import { getProducts } from '@/lib/products'
import { Cart } from './cart'
export default async function Page() {
const products = await getProducts() // cached, part of the static shell
return (
<>
<ProductGrid products={products} />
<Suspense fallback={<p>Loading cart</p>}>
<Cart /> {/* reads cookies(), streams per request */}
</Suspense>
</>
)
}
```
Rules:
1. Put `'use cache'` at the top of an async function, component or file. Its arguments and closed-over values become part of the cache key, so they must be serializable.
2. Don't call `cookies()`, `headers()` or read `searchParams` inside a cached scope. Read them outside and pass plain values in.
3. Wrap anything that reads request data, or that is uncached, in `<Suspense>`. Otherwise the build reports that uncached data was accessed outside a Suspense boundary.
4. For values that must differ per request but don't come from request data (`Date.now()`, `crypto.randomUUID()`), call `await connection()` from `next/server` first, inside a Suspense boundary.
5. Tag anything a write can change, and invalidate the tag in the Server Action that writes it.
6. `generateStaticParams` must return at least one entry when Cache Components is on.
## Section B: the earlier model (no cacheComponents flag)
`fetch` is not cached by default (it hasn't been since Next.js 15). Opt in for each request or each segment.
```tsx
// cache this request for an hour and tag it
const res = await fetch('https://api.example.com/products', {
next: { revalidate: 3600, tags: ['products'] },
})
// cache until invalidated
const res2 = await fetch(url, { cache: 'force-cache' })
```
```tsx
// app/blog/page.tsx: segment config
export const revalidate = 600 // seconds; false means cache indefinitely
// export const dynamic = 'force-dynamic' // render on every request
```
For non-fetch data (ORM calls), the page is static unless it reads request data. Use `revalidate` segment config to refresh it, or `export const dynamic = 'force-dynamic'`.
## Invalidation (both models)
| After a write you want | Call (from `next/cache`) |
|---|---|
| The user who wrote it to see it right away | `updateTag('products')` inside the Server Action |
| Everyone to get fresh data soon, serving stale meanwhile | `revalidateTag('products', 'max')` |
| A specific page re-rendered | `revalidatePath('/shop')` |
| The current page re-rendered without touching caches | `refresh()` inside the Server Action |
`updateTag` and `refresh` work only in Server Actions. `revalidateTag` and `revalidatePath` also work in Route Handlers, for example a CMS webhook.
## Fetching patterns
- Fetch in the Server Component that renders the data. Don't add an internal API route just to call it from your own server code.
- Start independent requests together: `const [a, b] = await Promise.all([getA(), getB()])`.
- Pass a promise to a client component and unwrap it with `use()` when the client part should stream in.
- Wrap a database helper in React's `cache()` to dedupe calls within one render. This is separate from `'use cache'`, which persists across requests.
## Diagnosing stale or surprising data
1. Run `next build` and read the route table. It marks each route as static, dynamic or partially prerendered.
2. Data stale after a write: find the tag or path the write invalidates, and check it matches the one the read uses. A missing `cacheTag` is the most common cause.
3. Page dynamic when you expected static: search the tree for `cookies()`, `headers()`, `searchParams` or `connection()` outside Suspense.
4. Build fails on segment config: you are on Cache Components with section B exports. Replace `revalidate` with `cacheLife`, and remove `dynamic`.
## Checks before you finish
- The model is identified and only that model's APIs are used.
- Every cached read that a write can change has a tag, and that write invalidates it.
- No request data is read inside `'use cache'`.
- Personalised data is never cached in a shared scope.
Wersje
W bazie nie ma innych skilli o nazwie nextjs-data-caching.
Migawka bazy z 13.11.2026 · wersja paczki 7b69900c94c4 (skrót SHA-256 całej paczki)
Pobrania w ostatnich 30 dniach: 0 · ranking
Zgłoś problem z tym skillem: szkodliwa treść, błędny opis albo prośba autora o usunięcie.
Podobne skille
- 100nextjs-server-components
Stosuje komponenty serwerowe w Next.js App Router: pobieranie danych bez wodospadów, strumieniowanie z Suspense i małe granice klienta. - 100nextjs-caching
Konfiguruje warstwy cache Next.js, unieważnianie i API cache components: cache fetch, use cache, tagi oraz debugowanie nieaktualnych danych. - 100nextjs-data-fetching
Wdraża Fetch API, cache i strategie rewalidacji w Next.js: pobieranie danych, konfiguracja zachowania cache i rewalidacja. - 95nextjs-app-router
Prowadzi przez Next.js 14+ z App Routerem: Server Components, routing, pobieranie danych, cache, server actions, streaming, Suspense i wdrażanie. - 100nextjs-app-router
Buduje i przegląda kod Next.js App Router: granice komponentów serwerowych i klienckich, pobieranie danych, cache i rewalidacja, streaming z Suspense oraz mutacje przez Server Actions. - 90nextjs-ppr-patterns
Opisuje strony App Router w Next.js 16 mieszające treść statyczną i dynamiczną z Partial Prerendering w modelu Cache Components: cacheComponents, granice Suspense, dyrektywa use cache, cacheLife i cacheTag.