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 10 / Topic 4 of 6

"use cache" at the UI level — caching the whole component

"use cache" doesn't have to sit inside a separate data-fetching function. Section 2 cached getDailyStats(), a plain function; this section caches an entire Server Component's body instead:

// harness/app/mandi/top-categories/page.tsx
export default async function TopCategoriesPage() {
  "use cache";
  const categories = await getTopCategories(); // plain, uncached data function
  return <main>{/* ...renders categories as a sorted <ol>... */}</main>;
}

getTopCategories() itself has no cache directive of its own — only the component calling it does. Verified for real: the first request pays the full 500ms simulated query cost, every request after that is fast, because the component's entire rendered output, not just the data call inside it, is what got cached:

$ curl -s -o /dev/null -w "%{time_total}s\n" http://localhost:PORT/mandi/top-categories   # first request
0.519s
$ curl -s -o /dev/null -w "%{time_total}s\n" http://localhost:PORT/mandi/top-categories   # second request
0.018s

This is the same distinction the Next.js docs draw between "data-level" and "UI-level" caching — both are the same directive, applied at a different point in the tree, and this part has now built and verified a real example of each.

Full file: examples/05-top-categories-page.tsx.

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

Search the guide