Data Fetching and Caching in Next.js

Fetch data on the server, decide what is static or dynamic, and revalidate cached data so pages stay fast and fresh.

Intermediate · about 6 hours · difficulty 4/5

Learn first: Server and Client Components

Next.js gives you several ways to fetch and cache data. The defaults have changed across versions, so always check the docs for your version, but the underlying questions stay the same.

Fetching on the server

Server components can be async: await a database query or a fetch call directly in the component. Fetch data in parallel where possible, and wrap slow parts in Suspense so the rest of the page streams first.

Static vs dynamic

  • Static: rendered at build time or on first request and reused. Great for company profiles, blog posts and landing pages.
  • Dynamic: rendered per request. Needed for anything that depends on cookies, the logged-in user or real-time data.
  • Reading cookies, headers or searchParams usually makes a route dynamic.

Revalidation

  • Time-based: revalidate every N seconds for data that changes slowly, like a list of companies.
  • On-demand: call revalidatePath or revalidateTag after a mutation so the next visitor sees fresh data.

Pitfalls

  • Assuming data is fresh when it is actually cached, or the opposite, making every page dynamic and slow.
  • Request waterfalls: awaiting one fetch, then the next, when they are independent.
  • Caching user-specific data in a shared cache.

You are ready to move on when you can explain, for every route in your placement portal, whether it is static or dynamic, how long its data is cached, and what triggers revalidation.

Key concepts

  • Async server components
  • Parallel fetching and waterfalls
  • Static vs dynamic rendering
  • Time-based revalidation
  • revalidatePath and revalidateTag
  • Streaming with Suspense
  • generateStaticParams

Mini project

Make the companies list static with hourly revalidation, company pages pre-rendered with generateStaticParams, and the student dashboard fully dynamic. Document each choice in the README.

Check yourself

An admin updates a company's package but students still see the old CTC for an hour. Which two fixes could you apply, and which is better here?

Free resources