Data Fetching & Revalidation in Next.js
Data fetching in Next.js App Router: async/await in Server Components, extended fetch API, time-based revalidation, and on-demand cache tagging.
Data Fetching & Revalidation in Next.js
In the App Router, data fetching is performed directly inside async Server Components using async/await or through the native fetch() API with granular caching controls.
1. Direct Backend & Database Queries
Server Components can query databases (Prisma, Drizzle, PostgreSQL) directly with zero API latency overhead:
import { prisma } from "@/lib/prisma";
export default async function DashboardPage() {
const users = await prisma.user.findMany({
orderBy: { createdAt: "desc" },
take: 10,
});
return (
<div>
<h2>Recent Users ({users.length})</h2>
</div>
);
}2. Extended fetch() Caching Options
Next.js extends the native fetch Web API with caching and revalidation controls:
1. Static Cached Fetch (Default / Build-Time SSG)
// Cached indefinitely until manual revalidation:
const res = await fetch("https://api.example.com/docs", {
cache: "force-cache",
});2. Time-Based Incremental Static Regeneration (ISR)
// Revalidates data at most once every 60 seconds:
const res = await fetch("https://api.example.com/crypto-prices", {
next: { revalidate: 60 },
});3. Dynamic Server-Side Rendering (SSR on every request)
// Never caches; fetches fresh on every incoming request:
const res = await fetch("https://api.example.com/live-stock", {
cache: "no-store",
});3. On-Demand Revalidation (revalidateTag & revalidatePath)
Purge cached data instantly when data changes (e.g. after a Server Action mutation):
import { revalidateTag, revalidatePath } from "next/cache";
export async function publishArticle(formData: FormData) {
"use server";
// 1. Mutate database...
// 2. Purge cache for tagged fetches:
revalidateTag("articles-list", "max");
// 3. Or purge cache for a specific route:
revalidatePath("/docs");
}Related Topics
Client Components ('use client') in Next.js
When and how to use Client Components: the 'use client' directive, client-server component boundaries, and passing server components as children.
Loading UI and Streaming with Suspense in Next.js
Creating instant loading states in Next.js: loading.tsx conventions, React Suspense boundaries, and progressive SSR streaming.