0
Challenge · 20Rendering & Caching
use cache & cacheLife
Cachingused in ~72% of apps
This is the single point most tutorials get backwards because they were written for Next.js 14. If you assume your data is cached and it isn't (or vice-versa), you ship stale prices to users or hammer your database on every request. Knowing exactly what `"use cache"` keys on, and what `cacheLife` controls, is the difference between a senior answer and a guess.
Core idea. Next.js 16 is `DYNAMIC by default`: every page, fetch, and DB read re-runs at request time unless you say otherwise. You opt INTO caching with the `"use cache"` directive — the compiler derives the cache key automatically from the function's serializable arguments (and closed-over values). `cacheLife` then sets how long that entry is fresh, stale, and alive. This is the inverse of Next.js 14's 'cached by default, opt out'.
poke it, drag it,
try to break it
try to break it
no slides here.
you drive.
you drive.
Playground, this isn't a screenshot
<ViewCount />no data yet
—
Press Visit to mount the component.
fetch
SWR CACHE (key: /views)
(empty)
SERVER (live, drifts)
1,280
visits: 0
// SWR activity log…
First visit blocks on the network. Every visit after serves the cached (stale) value instantly, then revalidates in the background and swaps in fresh data — that is the whole name made literal.
↑ play a little first, then dig in