Next.jsHard
How do you implement caching strategies in Next.js?
Decide per piece of data how fresh it must be, then use the matching tool: prerender static content, cache shared data with "use cache" plus cacheLife and cacheTag, stream per-request data behind <Suspense>, deduplicate within a request with React.cache, and invalidate on writes with updateTag or revalidateTag. This describes Cache Components, the default for new apps in Next.js 16.4.
A practical decision table
| Data | Strategy |
|---|---|
| Marketing copy, docs | Static: no I/O, prerendered automatically |
| CMS content | "use cache" + cacheLife("max") + cacheTag, webhook calls revalidateTag(tag, "max") |
| Product catalog | "use cache" + cacheLife("hours") |
| User's own edits | updateTag in the Server Function for read-your-writes |
| Per-user dashboard | Request-time inside <Suspense>; cache per user with arguments or "use cache: private" |
| Live prices | Uncached, streamed; or client polling with SWR |
Layers to know
- Request memoization:
fetchandReact.cachededupe within one render. use cacheentries: in-memory LRU per server by default;"use cache: remote"for a shared durable store (configurecacheHandlers).- Prerendered output: the static shell and ISR pages (served by a CDN).
- Client cache: the router keeps RSC payloads for the
staletime (minimum 30 seconds), and keeps visited pages in<Activity>. - HTTP/CDN caching:
Cache-Controlheaders on Route Handlers and assets.
Rules
- Pair every
"use cache"with acacheLife. - Never cache per-user data under a shared key.
- Prefer tags over
revalidatePath; they are precise. - Keep request-time reads deep in the tree so the static shell is large.
- Cache entries are scoped to a deployment; use
fetchcache orunstable_cacheif data must survive deploys.
Without Cache Components
fetch options (force-cache, next.revalidate, next.tags), unstable_cache, and segment config (revalidate, dynamic).
Code example
tsx
1// lib/data.ts2import "server-only";3import { cache } from "react";4import { cacheLife, cacheTag } from "next/cache";5import { db } from "@/lib/db";67// Shared across users, revalidated on CMS publish8export async function getArticle(slug: string) {9 "use cache";10 cacheLife("max");11 cacheTag(`article:${slug}`);12 return db.article.findUnique({ where: { slug } });13}1415// Shared, time-based refresh16export async function getCategories() {17 "use cache";18 cacheLife("hours");19 cacheTag("categories");20 return db.category.findMany();21}2223// Per user: user id is part of the cache key24export async function getUserStats(userId: string) {25 "use cache";26 cacheLife("minutes");27 cacheTag(`stats:${userId}`);28 return db.stats.findUnique({ where: { userId } });29}3031// Per request dedupe for uncached reads32export const getCart = cache(async (cartId: string) => db.cart.findUnique({ where: { id: cartId } }));
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the Next.js set.