A frustration I have with the current NextJS cache behavior is that there is no built in way to have a time-based cache without serving stale data. For my use case, this is not valid.
From what I can tell, the new (upcoming) "use cache" directive and cacheLife function may solve my problem, but I can't tell for sure from the docs.
If I did the following:
// /app/some-route/route.js
"use cache";
export const GET = async (req) => {
cacheLife({
stale: 30, // 30 seconds
revalidate: 30, // 30 seconds
expire: 30, // 30 seconds
});
// route handler logic
};
What is the behavior? Specifically, will it server stale data at the first request after expiry, or will it switch to dynamic immediately after the data is stale (never serving stale data)?