Optimize React package using per-request "cache" #346

Closed
opened 2026-03-13 07:42:36 -05:00 by GiteaMirror · 6 comments
Owner

Originally created by @ariesclark on GitHub (Dec 6, 2024).

Is your feature request related to a problem? Please describe.

import { betterAuth } from "better-auth";

const { api: { getSession, /* or any call */} } = betterAuth(...);
...

await getSession({ headers: await headers() }); // database call.
await getSession({ headers: await headers() }); // database call.
await getSession({ headers: await headers() }); // database call.

Describe the solution you'd like

import { betterAuth } from "better-auth/next-js"; // or React & used by Next.js too.

// Calls to fetch data are automatically cached using https://react.dev/reference/react/cache.
const { api: { getSession, /* or any call */ } } = betterAuth(...);
...

await getSession({ headers: await headers() }); // database call.
await getSession({ headers: await headers() }); // reuse previous value.
await getSession({ headers: await headers() }); // reuse previous value.

Describe alternatives you've considered

import { cache } from "react"; // important.
import { nextCookies, betterAuth } from "better-auth/next-js";

const { api: { getSession: _getSession, /* or any call */ } } = betterAuth(...);

// for every API exposed by better-auth.
const getSession = cache(_getSession);

...

await getSession({ headers: await headers() }); // database call.
await getSession({ headers: await headers() }); // reuse previous value.
await getSession({ headers: await headers() }); // reuse previous value.

Additional context
https://react.dev/reference/react/cache

Originally created by @ariesclark on GitHub (Dec 6, 2024). **Is your feature request related to a problem? Please describe.** ```ts import { betterAuth } from "better-auth"; const { api: { getSession, /* or any call */} } = betterAuth(...); ... await getSession({ headers: await headers() }); // database call. await getSession({ headers: await headers() }); // database call. await getSession({ headers: await headers() }); // database call. ``` **Describe the solution you'd like** ```ts import { betterAuth } from "better-auth/next-js"; // or React & used by Next.js too. // Calls to fetch data are automatically cached using https://react.dev/reference/react/cache. const { api: { getSession, /* or any call */ } } = betterAuth(...); ... await getSession({ headers: await headers() }); // database call. await getSession({ headers: await headers() }); // reuse previous value. await getSession({ headers: await headers() }); // reuse previous value. ``` **Describe alternatives you've considered** ```ts import { cache } from "react"; // important. import { nextCookies, betterAuth } from "better-auth/next-js"; const { api: { getSession: _getSession, /* or any call */ } } = betterAuth(...); // for every API exposed by better-auth. const getSession = cache(_getSession); ... await getSession({ headers: await headers() }); // database call. await getSession({ headers: await headers() }); // reuse previous value. await getSession({ headers: await headers() }); // reuse previous value. ``` **Additional context** https://react.dev/reference/react/cache
Author
Owner

@arthberman commented on GitHub (Dec 18, 2024):

+1 should be by default

@arthberman commented on GitHub (Dec 18, 2024): +1 should be by default
Author
Owner

@developedbyed commented on GitHub (Feb 13, 2025):

I initially turned on betterAuth.cookieCaching but I’ve noticed it was still doing db calls when I’d call getSession, would love someone to clarify 👍

@developedbyed commented on GitHub (Feb 13, 2025): I initially turned on betterAuth.cookieCaching but I’ve noticed it was still doing db calls when I’d call getSession, would love someone to clarify 👍
Author
Owner

@nktnet1 commented on GitHub (Feb 23, 2025):

Relevant discussion in #986

@nktnet1 commented on GitHub (Feb 23, 2025): Relevant discussion in #986
Author
Owner

@Kinfe123 commented on GitHub (Apr 11, 2025):

further discussion moved to the #986

@Kinfe123 commented on GitHub (Apr 11, 2025): further discussion moved to the #986
Author
Owner

@lohrm-stabl commented on GitHub (Sep 23, 2025):

Sorry, but #986 is for client-side while this issue is talking about server-side, no? As far as I can tell there is still no solution besides wrapping every better-auth method with cache()?

@lohrm-stabl commented on GitHub (Sep 23, 2025): Sorry, but #986 is for client-side while this issue is talking about server-side, no? As far as I can tell there is still no solution besides wrapping every better-auth method with `cache()`?
Author
Owner

@Kinfe123 commented on GitHub (Sep 23, 2025):

Yeah we don't have any built support for caching those values but it can stitched with you custom solution for caching your data by wrapping framework level api or any service.

@Kinfe123 commented on GitHub (Sep 23, 2025): Yeah we don't have any built support for caching those values but it can stitched with you custom solution for caching your data by wrapping framework level api or any service.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#346