completefrontend

Predict the outputHard

A webhook revalidates one price tag with "max" and another with { expire: 0 }. What do the next three pages show?

Every page of the tea shop reads its prices through getPrice, cached for max and tagged price:<slug>. The list at /teas shows Green tea and Mint tea, and each tea has its own page. When the supplier changes prices, it posts them to a Route Handler, which saves them and revalidates Green tea's tag with "max" and Mint tea's, which is on sale, with { expire: 0 }. Right after the supplier's call, three visitors open /teas, /teas/green, and /teas/mint, in that order. What does each see?

app/layout.tsx
export const metadata = { title: "Leaf and Kettle" };
 
export default function RootLayout({ children }: LayoutProps<"/">) {
  return (
    <html lang="en">
      <body>
        <header>Leaf and Kettle</header>
        {children}
      </body>
    </html>
  );
}
data/prices.json
{"green":4,"mint":3}
lib/prices.ts
import { readFile, writeFile } from "node:fs/promises";
 
// A stand-in for the shop's database: a JSON file on the server's disk.
const FILE = "data/prices.json";
 
export async function readPrice(slug: string): Promise<number> {
  const prices: Record<string, number> = JSON.parse(await readFile(FILE, "utf8"));
  return prices[slug];
}
 
export async function setPrices(update: Record<string, number>) {
  const prices = JSON.parse(await readFile(FILE, "utf8"));
  await writeFile(FILE, JSON.stringify({ ...prices, ...update }));
}
lib/catalog.ts
import { cacheLife, cacheTag } from "next/cache";
import { readPrice } from "@/lib/prices";
 
export async function getPrice(slug: string) {
  "use cache";
  cacheTag(`price:${slug}`);
  cacheLife("max");
  return readPrice(slug);
}
app/teas/page.tsx
import { getPrice } from "@/lib/catalog";
 
export default async function TeasPage() {
  const [green, mint] = await Promise.all([getPrice("green"), getPrice("mint")]);
  return (
    <main>
      <h1>Our teas</h1>
      <ul>
        <li>Green tea: ${green}</li>
        <li>Mint tea: ${mint}</li>
      </ul>
    </main>
  );
}
app/teas/green/page.tsx
import { getPrice } from "@/lib/catalog";
 
export default async function GreenTeaPage() {
  const price = await getPrice("green");
  return (
    <main>
      <h1>Green tea</h1>
      <p>Price: ${price}</p>
    </main>
  );
}
app/teas/mint/page.tsx
import { getPrice } from "@/lib/catalog";
 
export default async function MintTeaPage() {
  const price = await getPrice("mint");
  return (
    <main>
      <h1>Mint tea</h1>
      <p>Price: ${price}</p>
    </main>
  );
}
app/api/prices/route.ts
import { revalidateTag } from "next/cache";
import { setPrices } from "@/lib/prices";
 
export async function POST(request: Request) {
  const prices = (await request.json()) as Record<string, number>;
  await setPrices(prices);
  // Green tea's new price can show a moment late; Mint tea is on sale, so its price can't.
  revalidateTag("price:green", "max");
  revalidateTag("price:mint", { expire: 0 });
  return Response.json({ updated: Object.keys(prices) });
}

It's a project create-next-app made (Next.js 16.4.3, TypeScript, the App Router, and Cache Components on) with these files, built with next build and run with next start. What happens:

  1. You run next build and next start, and open /teas.
  2. The supplier posts {"green":5,"mint":4} to /api/prices.
  3. A visitor opens /teas.
  4. Another opens /teas/green.
  5. Another opens /teas/mint.

The answer

Work out what Next.js does at each step: what the build prints, what each request answers, and what the page shows. Then check.

Checking your sign-in…