Next.js Discord

Discord Forum

How to handle caching in nextjs?

Unanswered
Himalayan posted this in #help-forum
Open in Discord
HimalayanOP
How to handle cache in nextjs?
Need to turn on cache flag in config?
And do 'use cache'?

Whole page cache ideally
It's behind an auth tho. Different response for login (and paid) vs not.
So separate cache for wether it's a free response vs paid response

9 Replies

HimalayanOP
@aardani
@Himalayan How to handle cache in nextjs? Need to turn on cache flag in config? And do 'use cache'? Whole page cache ideally It's behind an auth tho. Different response for login (and paid) vs not. So separate cache for wether it's a free response vs paid response
You can still cache normally behind auth because nextjs's cache is mainly on the server side. This is beneficial because even if user isnt paid try to access paid resource, your auth guard blocks return while the cache is served for the next request
Simplest implementation if you dont have cacheComponents:true in the config is to wrap your data fetching method with unstable_cache. If you are useing fetch() then no need unstable_cache as you can directly configure cache in the fetch parameters
use cache is imo better if you have some part of the app that serves static public content and if u want to delve into partial prerendering.
I am using keyv library now which is similar concept
From what I understand there's only 2 ways.
Either unstable cache or
'use cache'
I don't have cache components true. Not sure if it will break stuff