Combine cache and up to date datas fetching
Unanswered
Australian Freshwater Crocodile posted this in #help-forum
Australian Freshwater CrocodileOP
Hello,
I'm looking for a way to always display up to date datas, but I have a problem which is sometimes API's can fail for some reason (such as rate limiting) and in that case I would use cache.
I've tried to use the revalidate props in getStaticProps and that almost work, but the problem is after revalidating time, the first user still have wrong datas, and he will generate a up-to-date one in the background, but this user will never saw it and that's the problem with some tricky values such as product price and availability.
I have 2 "ideas" but I don't know if it's feasible:
1) Do something like :
try {
fetchDatas();
} catch {
useCache()
}
2) Get the information that a user triggered a revalidation and replace the old page by the new one (or at least do window.reload()) when it's done
Have you any idea to do it, or any other ideas ?
Thanks in advance
I'm looking for a way to always display up to date datas, but I have a problem which is sometimes API's can fail for some reason (such as rate limiting) and in that case I would use cache.
I've tried to use the revalidate props in getStaticProps and that almost work, but the problem is after revalidating time, the first user still have wrong datas, and he will generate a up-to-date one in the background, but this user will never saw it and that's the problem with some tricky values such as product price and availability.
I have 2 "ideas" but I don't know if it's feasible:
1) Do something like :
try {
fetchDatas();
} catch {
useCache()
}
2) Get the information that a user triggered a revalidation and replace the old page by the new one (or at least do window.reload()) when it's done
Have you any idea to do it, or any other ideas ?
Thanks in advance
5 Replies
i wouldn't recoment window.reload and instead nextjs's useRouter and reload method because it keeps the state the same but updates the content
Australian Freshwater CrocodileOP
That's right, but anyways to use that I need to get the information that a user triggered a revalidation and it's done. And idk if I can get this information
In case you architecture allows it, it’s possible to trigger a revalidate when the source data change instead of when user requests it
https://nextjs.org/docs/pages/building-your-application/data-fetching/incremental-static-regeneration
https://nextjs.org/docs/pages/building-your-application/data-fetching/incremental-static-regeneration
@Jaga Santagostino In case you architecture allows it, it’s possible to trigger a revalidate when the source data change instead of when user requests it
https://nextjs.org/docs/pages/building-your-application/data-fetching/incremental-static-regeneration
Australian Freshwater CrocodileOP
Thanks for ur awnser, i've saw it already, but the problem is here : https://nextjs.org/docs/app/building-your-application/caching
With on demand revalidation it will not regenerate cache but only clear it, and will be generated next time a user go on the page if I correctly understand
So if the next user get an error during the generation it will result to a 500 error and not on a cache version of the page :/
With on demand revalidation it will not regenerate cache but only clear it, and will be generated next time a user go on the page if I correctly understand
So if the next user get an error during the generation it will result to a 500 error and not on a cache version of the page :/
Australian Freshwater CrocodileOP
Any ideas ? I find it strange that there's no way of knowing whether a refresh has been triggered from the app.