How to cache this function in nextjs 13.5
Answered
Southern rough shrimp posted this in #help-forum
Southern rough shrimpOP
i have this code . the problem is GetMovies takes 30sec i want to cache it to run only once every 24 hours
GetMovies have some Fetch request + supabase request + some logic (other request)
GetMovies have some Fetch request + supabase request + some logic (other request)
Answered by aardani
@Southern rough shrimp using unstable_cache or fetch to your own API
6 Replies
Within the file
Keep in mind that if you need to update your data for some reason you won't really be able to without building in custom cache busting. It would be better to try and reduce the query time, 30s is a problem
export const revalidate = 86400 // 24hrs in secondsKeep in mind that if you need to update your data for some reason you won't really be able to without building in custom cache busting. It would be better to try and reduce the query time, 30s is a problem
@Marchy Within the file
`export const revalidate = 86400 // 24hrs in seconds`
Keep in mind that if you need to update your data for some reason you won't really be able to without building in custom cache busting. It would be better to try and reduce the query time, 30s is a problem
Southern rough shrimpOP
i agree , and i'll try to fix the 30sec problem , however adding export const revalidate = 86400 simply doesnt do anything for some reasons , it keep callin GetMovies on every request , i tried npm run build npm start same thing
@Southern rough shrimp using unstable_cache or fetch to your own API
Answer
revalidate = 86400 only works if your page/route is statically rendered. however, due to the nature of needing to access user's cookies() at server component, those opts your page into dynamic SSR at request time.
Hence full router cache is out of the option (using page revalidation) so you need to look into Data Cache where result of a funciton call is cached, as opposed to the whole page
@aardani <@212048044714098688> using unstable_cache or fetch to your own API
Southern rough shrimpOP
thanks for your help . Your suggestion to use unstable_cache did the trick, and things are finally working , if anyone is having a similar problem this article is very useful https://alfonsusardani.notion.site/unstable_cache-from-next-cache-f300b3184d6a472ea5282543d50b9f02