Caching of server-side API
@Better Auth Are server side functions like auth.api.getSession cached or should I wrap them with react's cache()?
5 Replies
By default, Better Auth implements cookie caching (if you enable it in your config) to avoid unnecessary database calls; however, this is different from React’s per-request caching. In other words, server-side functions like auth.api.getSession aren’t automatically wrapped in React’s cache. If you’re using a framework like Next.js and want to avoid making multiple getSession calls within the same request, it’s a good idea to wrap the function with React’s cache() (as illustrated in [8]). This approach helps you reuse previously fetched values, further optimizing performance.
Sources:
[8] Optimize React package using per-request "cache"
GitHub
better-auth/better-auth
The most comprehensive authentication framework for TypeScript - better-auth/better-auth
GitHub
Optimize React package using per-request "cache" · Issue #781 · b...
Is your feature request related to a problem? Please describe. import { betterAuth } from "better-auth"; const { api: { getSession, /* or any call */} } = betterAuth(...); ... await getSe...
@Better Auth so closing of this issue was unjustified? https://github.com/better-auth/better-auth/issues/781#issuecomment-3323836139
GitHub
Optimize React package using per-request "cache" · Issue #781 · b...
Is your feature request related to a problem? Please describe. import { betterAuth } from "better-auth"; const { api: { getSession, /* or any call */} } = betterAuth(...); ... await getSe...
Hi there! I couldn’t find any details related to issue #781 or the referenced comment (3323836139) in our provided context documents. Could you please clarify which behavior or aspect of the issue you’re referring to? That way, I can check against our guidelines or known discussions regarding Better Auth and give you a more informed answer [1].
Sources:
[1] v1.3.13 failed to release
GitHub
better-auth/better-auth
The most comprehensive authentication framework for TypeScript - better-auth/better-auth
GitHub
v1.3.13 failed to release · Issue #4772 · better-auth/better-auth
To Reproduce https://github.com/better-auth/better-auth/actions/runs/17848983106 Sounds like the commit it not present on main, somehow. @himself65 Current vs. Expected behavior Expected last npm t...
@Better Auth basically, you would have to wrap every single better auth server api call in cache() right
Not necessarily. While caching is a great optimization for reducing database queries for many read-only endpoints (like getting sessions or listing users) [1], you don’t need to wrap every single Better Auth server API call in cache(). For example, for endpoints that handle sensitive changes—such as password updates—the framework intentionally bypasses cookie caching to ensure you always get the most up-to-date session verification [4], [5]. In short, use caching where it helps performance without compromising security or data freshness.
Sources:
[1] Optimizing for Performance | Better Auth
[4] Sensitive auth functions should ignore the cookie cache in session middleware
[5] Session Management | Better Auth
GitHub
better-auth/better-auth
The most comprehensive authentication framework for TypeScript - better-auth/better-auth
Optimizing for Performance | Better Auth
A guide to optimizing your Better Auth application for performance.
GitHub
Sensitive auth functions should ignore the cookie cache in session ...
Is this suited for github? Yes, this is suited for github Is your feature request related to a problem? Please describe. No response Describe the solution you'd like Auth functions like /change...