giuseppe-trisciuoglio/developer-kit / plugins/developer-kit-typescript/skills/nextjs-data-fetching
nextjs-data-fetching
VerifiedProvides Next.js App Router data fetching patterns including SWR and React Query integration, parallel data fetching, Incremental Static Regeneration (ISR), revalidation strategies, and error boundaries. Use when implementing data fetching in Next.js applications, choosing between server and client fetching, setting up caching strategies, or handling loading and error states.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 8.7 KB |
| references/caching-strategies.md | markdown | 2.7 KB |
| references/client-fetching.md | markdown | 3.2 KB |
| references/data-fetching.md | markdown | 2.8 KB |
| references/error-boundaries.md | markdown | 2.2 KB |
| references/error-loading-states.md | markdown | 3.3 KB |
| references/react-query.md | markdown | 8.2 KB |
| references/server-actions.md | markdown | 3.3 KB |
references/caching-strategies.md
View on GitHub# Caching and Revalidation Strategies
## Time-based Revalidation (ISR)
Use time-based revalidation when stale data is acceptable for a bounded period.
```tsx
async function getPosts() {
const res = await fetch('https://api.example.com/posts', {
next: {
revalidate: 60,
},
});
return res.json();
}
```
Choose the revalidation interval based on how often the data changes.
## On-Demand Revalidation
Use Route Handlers or Server Actions with `revalidateTag()` or `revalidatePath()` when data should refresh immediately after a write.
```tsx
// app/api/revalidate/route.ts
import { revalidateTag } from 'next/cache';
import { NextRequest } from 'next/server';
export async function POST(request: NextRequest) {
const tag = request.nextUrl.searchParams.get('tag');
if (tag) {
revalidateTag(tag);
return Response.json({ revalidated: true });
}
return Response.json({ revalidated: false }, { status: 400 });
}
```
Keep invalidation tags stable and descriptive so read and write paths stay coordinated.
## Tag Cached Data for Selective Invalidation
Attach cache tags when the same data source is read in multiple places.
```tsx
async function getPosts() {
const res = await fetch('https://api.example.com/posts', {
next: {
tags: ['posts'],
revalidate: 3600,
},
});
return res.json();
}
```
Use a small set of predictable tags instead of dynamically generating unnecessary tag variants.
## Opt Out of Caching
Disable caching for highly dynamic or user-specific data.
```tsx
async function getRealTimeData() {
const res = await fetch('https://api.example.com/data', {
cache: 'no-store',
});
return res.json();
}
export const dynamic = 'force-dynamic';
```
Use `no-store` intentionally because it trades performance for freshness.
## Cache Selection Checklist
- Use ISR when the page can tolerate bounded staleness.
- Use tags when a mutation needs to refresh multiple consumers.
- Use `no-store` for real-time, user-specific, or security-sensitive responses.
- Avoid sharing cache entries across different user contexts.
## Example: Blog Page with ISR
**Input:** Create a blog page that fetches posts and updates every hour.
```tsx
// app/blog/page.tsx
async function getPosts() {
const res = await fetch('https://api.example.com/posts', {
next: { revalidate: 3600 },
});
return res.json();
}
export default async function BlogPage() {
const posts = await getPosts();
return (
<main>
<h1>Blog Posts</h1>
{posts.map((post) => (
<article key={post.id}>
<h2>{post.title}</h2>
<p>{post.excerpt}</p>
</article>
))}
</main>
);
}
```
**Output:** The page is cached and revalidated every hour.
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack." | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |
| ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |