Part 10 / Topic 2 of 6
"use cache" at the data level, and what it actually buys you
Mandi Fresh's "today's stats" — orders today, revenue today — stands in for a real aggregation query that would genuinely hurt to run on every request. lib/stats.ts caches it directly:
// harness/app/mandi/lib/stats.ts
import { cacheLife, cacheTag } from "next/cache";
export async function getDailyStats(): Promise<DailyStats> {
"use cache";
cacheLife("hours");
cacheTag("mandi-daily-stats");
return runAggregationQuery(); // 600ms simulated aggregation, includes new Date().toISOString()
}
// harness/app/mandi/stats/page.tsx
export default async function StatsPage() {
const stats = await getDailyStats();
return <main>{/* ...renders stats.ordersToday, stats.computedAt, etc... */}</main>;
}
Notice what's missing compared to section 1: no <Suspense> anywhere on this page. "use cache" is permission for Cache Components to bake this result straight into the static shell at build time, the same way it bakes in any other predictable value — so there's nothing left that needs streaming. Three requests in a row, verified against a real next build && next start:
$ curl -s http://localhost:PORT/mandi/stats | grep -o '<p id="computed-at">[^<]*</p>'
<p id="computed-at">Computed at: 2026-12-01T11:07:33.164Z</p>
<p id="computed-at">Computed at: 2026-12-01T11:07:33.164Z</p>
<p id="computed-at">Computed at: 2026-12-01T11:07:33.164Z</p>
The exact same timestamp, every time — the 600ms aggregation genuinely ran once, at build time, and every request since has read the cached result. The response headers confirm it from the server's own account, not just an inference from three identical timestamps:
$ curl -s -D - -o /dev/null http://localhost:PORT/mandi/stats
x-nextjs-cache: HIT
x-nextjs-prerender: 1
x-nextjs-stale-time: 300
Cache-Control: s-maxage=3600, stale-while-revalidate=82800
s-maxage=3600 is cacheLife('hours')'s revalidate value (3600 seconds = 1 hour) made literal; stale-while-revalidate=82800 is expire (86400s = 1 day) minus revalidate (3600s) = 82800 — the window where a stale copy can still be served while a fresh one regenerates in the background. Both numbers are read directly from a real response header, not restated from the docs.
Full files: examples/02-stats-module.ts, examples/03-stats-page.tsx.
Done here? The practice set for Part 10 — including the solutions — is on the part page.