Skip to content

Lesson 5 of 9 · 27m

Revalidation as a Product Decision

Time-based and on-demand revalidation are freshness contracts: choose them from user expectations, mutation paths, and cost — not from superstition.

Revalidation is not a cache knob; it is a promise. "This page can be ten minutes old" is a product decision. "After an editor publishes, the article must update immediately" is an operational dependency. Teams get into trouble when those promises are implicit and every mutation forgets half the pages it affects.

Time-based revalidation fits naturally aging data

export const revalidate = 300;

export default async function PricingPage() {
  const plans = await getPlans();
  return <PricingTable plans={plans} />;
}

Use it when staleness is bounded and tolerable: documentation, catalogs, public pricing, leaderboards that can lag. Five minutes is often a better answer than a distributed invalidation system.

Time-based does not mean every visitor waits five minutes. It means the cache regenerates after the revalidation window according to the rendering platform's stale-while-revalidate behavior. In high traffic, most users never see the regeneration cost.

On-demand invalidation fits mutation-driven data

import { revalidatePath, revalidateTag } from "next/cache";

export async function publishArticle(id: string) {
  await db.article.update({ where: { id }, data: { published: true } });
  revalidateTag(`article:${id}`);
  revalidatePath("/articles");
}

Tags scale better than paths because one conceptual object can appear in many routes: article pages, author pages, search cards, RSS. Fetch or data functions opt into tags:

await fetch(`${api}/articles/${id}`, { next: { tags: [`article:${id}`] } });

For internal database calls, tags only help if your data-access layer integrates them through cache utilities. revalidateTag does not magically know about arbitrary PostgreSQL reads.

Invalidation graphs rot

The failure mode is familiar: marketing edits a hero image, the home page updates, the landing page does not, support hears "the site is broken." The fix is not "call revalidate everywhere." It is a small invalidation map owned with the data layer:

article.updated -> tag article:<id>, tag author:<authorId>, path /articles
plan.updated    -> tag plans, path /pricing, path /signup
user.updated    -> no public invalidation; dynamic private UI reads live data

Write it down. When a new surface consumes the object, update the map in the same PR.

Over-invalidation is a latency bug

revalidatePath("/", "layout") invalidates broad sections of a site. Sometimes that is correct — navigation content changed. Usually it is panic. Broad invalidation turns one editor click into a thundering rebuild of popular routes.

Field notes

  • Revalidate after the transaction commits, not before. Invalidating a page that then reads the old row gives you confidently stale output.
  • Webhook-driven invalidation must authenticate the webhook and be idempotent.
  • A cache stampede after broad invalidation is real. Stagger regeneration or use time-based jitter for fleets of similar pages.
  • Personalized pages are usually cheaper as dynamic pages than as fragile invalidation puzzles.

Was this lesson helpful?

Sign in to rate