cacheLife: never serve stale data
Unanswered
Brown bear posted this in #help-forum
Brown bearOP
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)
If I did the following:
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)?
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)?
1 Reply
Brown bearOP
Shoot, trying to test this out and it doesn't even work. This:
gives:
While this:
works fine...
"use cache";
export const GET = async () => {
return new Response("test");
};
gives:
[Error: No response is returned from route handler '<path>/app/some-route/route.ts'. Ensure you return a `Response` or a `NextResponse` in all branches of your handler.]
While this:
export const GET = async () => {
return new Response("test");
};
works fine...