Does next-shared-cache support stale-while-revalidate? #489
-
Hi, In my project when the key/value has expired, I noticed the next request rendring was really slow. because next.js will fetch data from backend server and write to redis by next-share-cache. Next data cache has provided a |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
Hey, @alex-guoba. You can use the CacheHandler.onCreation(async () => {
const handler = {
// ...
};
return {
handlers: [handler],
ttl: { estimateExpireAge: (staleAge) => staleAge * 10 }, // 10 time more than stale age
// or set fixed expire age in seconds by doing like this
// ttl: { estimateExpireAge: () => 60 * 60 * 24 * 365 }, // 1 year
};
}); Here is the documentation for the I hope you find this helpful! |
Beta Was this translation helpful? Give feedback.
-
Hi! I have a clarifying question. If you set the expiration age to be greater than the stale age, then when Next revalidates a cache entry, will it overwrite the current entry or create a new one? If Next overwrites the current entry, then there should be no problems with memory, even if you set the expiration date to a year. Because Next will overwrite entries in the cache, which is as close as possible to the stale while revalidate. Or I'm wrong? |
Beta Was this translation helpful? Give feedback.
Hey, @alex-guoba.
You can use the
estimateExpireAge
callback to estimate the expiration age of stale data. By default, the stale age is the same as the expiration age, which can cause long loading times when the data becomes stale. However, you can customize the expiration age or calculate it based on the stale age to improve loading times.Here is t…