Course map
Stage 1: App Router Foundations
Stage 2: The Rendering Model
Stage 3: Data, Caching and Mutations
Stage 4: Full-Stack Next.js
Stage 5: Production and Specialization

Part 11 / Topic 3 of 6

revalidateTag with "max" — stale-while-revalidate, on demand

Udupi Grande's menu snapshot is cached with cacheTag("swiggy-menu") (Part 10's own convention, reused here). The on-demand trigger lives in a Route Handler, because updateTag (section 5) can't be used outside a Server Action:

// harness/app/api/swiggy-menu/revalidate/route.ts
import { revalidateTag } from "next/cache";
import { setPrice } from "@/app/swiggy-menu/lib/menu";

export async function POST(request: Request) {
  const body = await request.json() as { id: string; price: number; mode: "background" | "immediate" };
  const updated = setPrice(body.id, body.price);
  if (body.mode === "immediate") {
    revalidateTag("swiggy-menu", { expire: 0 });   // section 4
  } else {
    revalidateTag("swiggy-menu", "max");            // this section
  }
  return Response.json({ revalidated: true, item: updated, mode: body.mode });
}

Verified for real, setting Masala Dosa's price from ₹90 to ₹77 in "background" mode:

$ curl -s http://localhost:PORT/swiggy-menu | grep -o '₹<!-- -->[0-9]*'   # baseline
₹90
$ curl -s -X POST http://localhost:PORT/api/swiggy-menu/revalidate -d '{"id":"m1","price":77,"mode":"background"}'
{"revalidated":true,"item":{"id":"m1","name":"Masala Dosa","price":77,"available":true},"mode":"background"}
$ curl -s http://localhost:PORT/swiggy-menu | grep -o '₹<!-- -->[0-9]*'   # immediately after
₹90
$ sleep 1
$ curl -s http://localhost:PORT/swiggy-menu | grep -o '₹<!-- -->[0-9]*'   # one second later
₹77

The request right after calling revalidateTag("swiggy-menu", "max") still shows the old price (₹90) — the installed docs' own words: "the next request for that data kicks off a revalidation and is served stale content while it runs." Only after the background recomputation has had time to finish (the 500ms simulated price-join inside getCachedMenu, plus a little slack) does the fresh price show up. The write itself (setPrice) happened instantly; what took a second to become visible was the read path's own cache, caught mid-"max"-profile stale window.

🔍 Going deeper — the same cacheLife numbers also drive this on their own, with no revalidateTag call at all: getCachedMenu's own cacheLife({ stale: 30, revalidate: 6, expire: 18 }) means a request landing more than 6 seconds after the last computed value, with no one calling revalidateTag at all, behaves identically to this section's manual trigger — served the old value, recomputed in the background. Polling this exact route every 2 seconds for 20 seconds, with no API call in between, reproduces the pattern on a fixed ~6–7 second cadence:

value=...04.112  took=0.041s    (t≈2s, within `stale`)
value=...04.112  took=0.039s    (t≈4s, within `stale`)
value=...11.390  took=0.552s    (t≈6s, crossed `revalidate` — recomputed)
value=...11.390  took=0.044s    (t≈8s, serving the new cached value)
value=...11.390  took=0.043s    (t≈10s)
value=...18.125  took=0.550s    (t≈12s, crossed `revalidate` again)

Worth naming plainly: hitting the Next.js server directly (no CDN in front, which is exactly this harness's setup), the request that crosses the revalidate boundary is the one that pays the ~550ms recompute cost itself, synchronously — not a background-only refresh that always leaves every request fast. "Stale-while-revalidate" describes the Cache-Control header's contract with a downstream cache (a real CDN would serve the old value here and refresh behind the scenes); it does not mean the origin server itself never blocks. Section 4's { expire: 0 } case removes any ambiguity about this by making the block explicit and total.

Full file: examples/02-swiggy-menu-revalidate-route.ts.

Done here? The practice set for Part 11 — including the solutions — is on the part page.

Search the guide