Skip to content

skillFrontendnextjsreact

Next.js Data Fetching

Chooses server components, caching and revalidation for each data need in the Next.js App Router, and fixes slow or stale pages.

npx workspacetemplates add nextjs-data-fetching --tool claude-code

Writes 1 file to .claude/skills/nextjs-data-fetching/

.claude/skills/nextjs-data-fetching/SKILL.md31 lines · 1.9 KBFile
---
name: nextjs-data-fetching
description: Chooses server components, caching and revalidation for each data need in the Next.js App Router, and fixes slow or stale pages.
---

# Next.js data fetching

Decide the freshness of each piece of data first; the code follows from that.

## Steps

1. Check the Next.js version in package.json and read its docs for caching defaults; they differ between 14 and 15 and later. Do not rely on memory.
2. For each data need ask: who sees it (everyone or one user), how fresh must it be (never changes, minutes, instant), and what changes it. Then choose:
   - Same for everyone and rarely changes: static, with `revalidate` seconds or tag-based revalidation.
   - Same for everyone but changes on events: tag the fetch and call `revalidateTag` or `revalidatePath` from the mutation.
   - Per user or per request (cookies, headers, searchParams): dynamic; keep it in a small component behind `<Suspense>`.
3. Fetch in server components with `async`/`await`. Start independent requests together with `Promise.all`; never await them in sequence. Pass promises down and unwrap them with `use` where useful.
4. Wrap slow sections in `<Suspense fallback>` with a `loading.tsx` so the shell streams first.
5. Deduplicate: call the same data function in several components freely, wrap non-fetch calls (ORM, SDK) in `cache()`.
6. Mutations use Server Actions or route handlers, validate input, and then revalidate the affected tags or paths.
7. Use client components only for interactivity. Fetch in them with a query library, and pass server-fetched initial data as props.
8. Check the build output: it lists which routes are static, dynamic or ISR. Confirm it matches the plan.

## Rules

- Never put secrets or server-only imports in client components; use `server-only`.
- Do not use `force-dynamic` to hide a caching bug.

## Report

A table of data, freshness choice, and invalidation, plus the build route summary.

What it does

In the App Router the same fetch can be static, cached, revalidated or dynamic depending on a few lines, and the defaults have changed between versions. This skill makes the agent decide deliberately for each piece of data.

It fetches on the server by default, removes waterfalls, streams slow parts behind Suspense, and ties every cache to a clear way of invalidating it.

Example

you › The dashboard page takes 3 seconds and shows old totals after saving an order

agent › Cause: three awaits in sequence and a cached totals fetch with no invalidation. Now Promise.all runs the 3 queries together, the recent-orders table streams behind Suspense, and the createOrder action calls revalidateTag("totals"). Build output: /dashboard is dynamic, /pricing stays static. Load time dropped to 0.9 s locally.

Questions

Does it work with the Pages Router?
This skill targets the App Router. For getServerSideProps and getStaticProps projects, say so and it will plan a migration instead.
Why does it check the version first?
Next.js changed what is cached by default between major releases, so advice that is right for one version causes stale pages in another.
  • Bundle Size Reduction

    Skilljavascript

    Free