Cache Invalidation

A cache is useful only if you can define when cached information should become invalid. Next.js provides mechanisms for invalidating cached data and routes after mutations.

A product editing workflow is a good example. After changing a product in the database, related product pages, category listings, and search results may need invalidation.

The important design question is dependency mapping: which cached resources depend on this mutation?

Invalidating a Path
'use server';

import { revalidatePath } from 'next/cache';

export async function updateProduct(id: string, name: string) {
  await database.products.update({ id, name });
  revalidatePath(`/products/${id}`);
  revalidatePath('/products');
}

After a mutation, related paths can be marked for fresh rendering.

Key Takeaways
  • Every cached resource should have a freshness strategy.

  • Mutations should identify affected cached views.

  • Overly broad invalidation can waste server resources.