Don't cache a live reading
Next.js can cache a function's return value. A turbine's current power is not that function. The shell can be an hour old. The number cannot.
Farsight's dashboards are Next.js in front of Kafka and TimescaleDB, and the number on the screen is a live plant reading. I have watched a correct cache make that kind of page worse. The HTML comes back fast, and the megawatt it shows is from whenever the cache was filled. For an operator that is not a performance win. It is a stale instrument.
Next.js, with Cache Components on, caches at the function or the component, not at the route. You mark an async function with use cache, you give it a cacheLife, and the arguments become the cache key. That is a good tool. It is the wrong tool for a value that is defined by being current.
Two lifetimes on one page
The asset name, the site, the layout of the page: those can be an hour old and nobody is misled. The power, the alarm state, the last sample: those are the product. I split them on that line and I do not let a convenience pull the live half into the cached half.
The shell is a cached server component. The live panel is outside it, behind Suspense, and the fresh read happens at request time. With Cache Components, an uncached async child without Suspense does not politely opt the route into dynamic rendering. It blocks the prerender. The boundary is what lets the shell flush and the reading stream in after.
import { Suspense } from "react";
import { cacheLife } from "next/cache";
async function AssetShell({ assetId }: { assetId: string }) {
"use cache";
cacheLife("hours");
const asset = await getAsset(assetId);
return (
<header>
<h1>{asset.name}</h1>
<p>{asset.site}</p>
</header>
);
}
async function LiveReading({ assetId }: { assetId: string }) {
const reading = await getLiveReading(assetId);
return <p>{reading.power}</p>;
}
export default async function AssetPage({
params,
}: {
params: Promise<{ id: string }>;
}) {
const { id } = await params;
return (
<>
<AssetShell assetId={id} />
<Suspense fallback={<p>Reading…</p>}>
<LiveReading assetId={id} />
</Suspense>
</>
);
}LiveReading is not cached. Where the update has to move without another document request, that panel is a client component on a socket. The server component is the frame. I don't stream a turbine through a cache key.
One footgun is worth naming because the API makes it easy. Cookies and headers are read outside the cached function and passed in as arguments. If the argument you pass is a user id, you now have an entry per user. Sometimes that is what you wanted. On a dashboard shared across a fleet it is a memory leak with a hit rate near zero. The cache key is every value the function closes over. I pass the asset id. I do not pass the session.
The listing page was not a cache story
On AdTeam the number I can point at is the product listing: about fifteen seconds, then about one second, on later benchmarks. The temptation is to tell that as a caching story. It isn't. The page was doing too many API calls, pulling rows it did not show, and starting expensive model calls during the load. Pagination, a narrower query, and a cache only for data every caller hits. The model work moved off the first paint. k6 was how we decided a change was real.
use cache around that old loader would have stored a fifteen-second mistake and served it quickly. A fast wrong page is a worse bug than a slow one, because people stop reporting it.
What I cache
I cache a function when I can say how old the value is allowed to be, and when the key is something coarser than a session. A marketing page, a catalog slice that changes when someone publishes, an asset header. I revalidate on the write, with a tag, rather than hoping the lifetime expires before anyone notices.
I do not cache a function whose correctness is "as of now". Clocks, balances, alarm state, a model's latest draft. Those go behind Suspense or on a socket, and the loading state is part of the UI, not an embarrassment to be hidden. The framework will cache whatever you mark. Deciding what is allowed to be old is the work.