revalidatePath() in Next.js
revalidatePath() is a cache control function in that forces a page to refresh its server data instead of using stale cached content.
What is revalidatePath()?
It tells Next.js to invalidate cached data for a specific route so the next visit fetches fresh data from the server.
How does it work?
import { revalidatePath } from "next/cache";
revalidatePath("/dashboard");The specified route cache is cleared, forcing fresh server rendering on the next request.
Why use revalidatePath()?
- Update UI after database changes
- Refresh dashboard content
- Keep cached pages accurate
- Improve performance with smart caching
Example — Refresh After Save
async function saveData() {
"use server";
await updateDatabase();
revalidatePath("/dashboard");
}After saving data, the dashboard cache refreshes automatically.
Real-World Example
When a user updates their profile, the profile page refreshes to show the latest information instead of cached data.
Interview Tip
revalidatePath() invalidates cached routes so Next.js fetches fresh server data, ensuring UI accuracy after updates.