mastering next.js 16 cache components - By Sourav Mishra (@souravvmishra)
let's look at the new 'use cache' directive in next.js 16. i'll show you how to cache your components easily.
caching in next.js was always a bit confusing. but in next.js 16, they dropped something amazing: cache components.
let's see how the new "use cache" directive works and how i use it to make my apps crazy fast.
what are cache components?
so basically, cache components let you opt-in to caching at the component level. no more messing with crazy fetch options. just drop "use cache" at the top.
why this is a big deal
before, caching was usually tied to fetch. now, it's super simple.
- granular control: cache specific parts of your UI.
- framework agnostic: works with any data, not just
fetch. - predictable: the boundary is very clear.
i noticed a 40% drop in database load just by switching to this.
how to use it
it's stupid easy. just add the directive to an async function or component.
// app/components/StockTicker.tsx
import { cacheLife } from 'next/cache';
export async function StockTicker({ symbol }: { symbol: string }) {
"use cache";
cacheLife("seconds"); // cache for a short bit
const price = await getStockPrice(symbol);
return (
<div className="p-4 border rounded-lg">
<h3 className="font-bold">{symbol}</h3>
<p className="text-xl">${price}</p>
</div>
);
}
what about cacheLife and cacheTag?
you get two new helpers for this:
cacheLife(profile): how long the data stays fresh.cacheTag(name): tagging the cache so you can clear it later.
using cacheLife
next.js gives you presets like seconds, minutes, hours, etc.
async function getData() {
"use cache";
cacheLife("hours");
return db.users.findMany();
}
i use cacheLife("minutes") a lot for internal dashboards to avoid spamming the database.
using cacheTag
when you mutate data and need to clear the cache, use tags.
import { cacheTag } from 'next/cache';
async function getUserProfile(id: string) {
"use cache";
cacheTag(`user-${id}`);
return db.user.find(id);
}
and then in your server action:
import { revalidateTag } from 'next/cache';
export async function updateUser(id: string, data: any) {
await db.update(id, data);
revalidateTag(`user-${id}`);
}
honestly, cache components make next.js development so much cleaner.
for more next.js stuff, read my debugging production css guide.