How do you use revalidatePath to refresh the cache for a specific page after content changes?
Short answer
You call revalidatePath with the exact path of the page you want to refresh, typically inside a server action or route handler right after a database update completes, and Next.js will discard the cached version of that page so the next visitor gets freshly rendered content.
Advertisement
AModel answer
You call revalidatePath with the exact path of the page you want to refresh, typically inside a server action or route handler right after a database update completes, and Next.js will discard the cached version of that page so the next visitor gets freshly rendered content.
'use server';
import { revalidatePath } from 'next/cache';
export async function publishPost(postId) {
await db.post.update({ where: { id: postId }, data: { published: true } });
revalidatePath('/blog');
revalidatePath(`/blog/${postId}`);
}Real-world example
A blogging platform revalidates both the blog listing page and the individual post page immediately after an author publishes an article, ensuring readers see the update right away on both pages.
Likely follow-ups
- Can revalidatePath refresh dynamic routes with different parameters at once?
- How quickly does the revalidated content become visible to users?
Related topics