Dynamic revalidation interval based on response
Unanswered
Japanese pilchard posted this in #help-forum
Japanese pilchardOP
What I'd like to be able to do is set the revalidation interval dynamically based on the content of a response, i.e. return it from the called function. Think something like this:
For comparison, the usual pattern is to pass the revalidation interval up front [1], which works great for most cases:
I've looked into the React
From what I've seen, the only other option is on-demand cache invalidation [4]. Unfortunately, this doesn't really fit the bill either, as it invalidates immediately, rather than after a nonzero amount of time.
[1] https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#time-based-revalidation
[2] https://react.dev/reference/react/cache
[3] https://nextjs.org/docs/app/api-reference/functions/unstable_cache
[4] https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#on-demand-revalidation
unstable_cache(
async () => {
// we want to skip the implicit cache here
const res = await (await fetch("/...", { cache: "no-store" })).json();
return {
data: res.data,
revalidate: res.revalidate
};
},
['/...']
);For comparison, the usual pattern is to pass the revalidation interval up front [1], which works great for most cases:
fetch('https://...', { next: { revalidate: 3600 } })I've looked into the React
cache [2] and Next unstable_cache [3], but the former isn't intended for persistent caching with intervals, and the latter expects the interval to be provided up front as well.From what I've seen, the only other option is on-demand cache invalidation [4]. Unfortunately, this doesn't really fit the bill either, as it invalidates immediately, rather than after a nonzero amount of time.
[1] https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#time-based-revalidation
[2] https://react.dev/reference/react/cache
[3] https://nextjs.org/docs/app/api-reference/functions/unstable_cache
[4] https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#on-demand-revalidation
3 Replies
Japanese pilchardOP
Hoping to bump this.
Unfortunately I don’t think that is possible
Japanese pilchardOP
@joulev Tx nonetheless