Best Practice for Reusing BetterAuth Session in Next.js App Router

I'm using BetterAuth in a Next.js app (app router). I'm calling getSession() in different parts of the app—like a layout and a page—and it's causing the session to be fetched multiple times per request. What's the best way to call getSession() only once per request and reuse the result across server components?
3 Replies
nikatune
nikatune2mo ago
you can use auth api like auth.api.getSession() on server components
The Untraceable
The Untraceable2mo ago
I have a getSession helper which is wrapped with react.cache I use that for when I need to verify auth not for data I need most updated
iatomic.btc
iatomic.btc2mo ago
Do share man. Also saw an x post about layout not rerendering on when routing so it's better to protect pages inside each individual page

Did you find this page helpful?