Does react 18 `cache()` cache across requests?

title, if I have a function wrapped with cache, does it cache across requests? or just dedupes calls in the same request https://nextjs.org/docs/app/building-your-application/data-fetching/caching#react-cache
Data Fetching: Caching
Learn about caching routes in Next.js.
3 Replies
whatplan
whatplan11mo ago
current, cache only dedupes calls in the same request a fetch-like cache is coming at some point I actually made a tweet about this (look at julius' replies to the second one in the thread) https://twitter.com/ethanniser/status/1676443763760144384?s=20
whatplan (@ethanniser)
its disappointing that granular caching is restricted to 'fetch' in @nextjs 13 being able to invalidate the exact points of data fetching that have changed is the coolest part of app router.. until you use your db client to query some data (the whole point of server components)
Twitter
jepcd
jepcd11mo ago
I'm not sure which way would be better, my use case currently is with next-auth in RSCs, I'm using getServerSession in a lot of server components, and without the cache it makes 2 db queries each time I use it This is what I have now, which works well
export const getCurrentSession = cache(async (): Promise<Session | null> => {
return await getServerSession(authOptions);
});
export const getCurrentSession = cache(async (): Promise<Session | null> => {
return await getServerSession(authOptions);
});
whatplan
whatplan11mo ago
Your usage looks correct, the idea with cache is not having to fetch once and pass down props everywhere, instead just get the data in the component itself and let the framework dedupe The new cache stuff is only about caching data between requests and granularity invalidating it, which probably wouldn’t be that useful for authentication which you want to check on every request