[GH-ISSUE #5263] Support NextJS 16 #18828

Closed
opened 2026-04-15 17:30:38 -05:00 by GiteaMirror · 5 comments
Owner

Originally created by @WorldOfMaze on GitHub (Oct 13, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/5263

Is this suited for github?

  • Yes, this is suited for github

Nope. Not a problem but would like to be able to run Better Auth cleanly on NextJS 16.

Describe the solution you'd like

NextJS 16 has recently been released. Due to changes in middleware from NextJS 15 to 16, Better Auth is not compatible with the newer version. Better Auth should be updated to work with the latest version on NextJS.

Describe alternatives you've considered

None.

Additional context

No response

Originally created by @WorldOfMaze on GitHub (Oct 13, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/5263 ### Is this suited for github? - [x] Yes, this is suited for github ### Is your feature request related to a problem? Please describe. Nope. Not a problem but would like to be able to run Better Auth cleanly on NextJS 16. ### Describe the solution you'd like [NextJS 16](https://nextjs.org/blog/next-16-beta) has recently been released. Due to changes in middleware from NextJS 15 to 16, **Better Auth** is not compatible with the newer version. **Better Auth** should be updated to work with the latest version on NextJS. ### Describe alternatives you've considered None. ### Additional context _No response_
GiteaMirror added the lockedenhancement labels 2026-04-15 17:30:38 -05:00
Author
Owner

@async-zenodev commented on GitHub (Oct 29, 2025):

yeah same

<!-- gh-comment-id:3460397599 --> @async-zenodev commented on GitHub (Oct 29, 2025): yeah same
Author
Owner

@jeancdevx commented on GitHub (Oct 29, 2025):

yup, i got errors when adding to next.config.ts
cacheComponents: true

and then trying to use:
const session = await auth.api.getSession({
headers: await headers()
})

as always, but this time throws

Image
<!-- gh-comment-id:3463357550 --> @jeancdevx commented on GitHub (Oct 29, 2025): yup, i got errors when adding to next.config.ts cacheComponents: true and then trying to use: const session = await auth.api.getSession({ headers: await headers() }) as always, but this time throws <img width="1233" height="715" alt="Image" src="https://github.com/user-attachments/assets/288fb55b-ddd4-474d-ab91-f8ba040841f4" />
Author
Owner

@fringe4life commented on GitHub (Oct 30, 2025):

yup, i got errors when adding to next.config.ts cacheComponents: true

and then trying to use: const session = await auth.api.getSession({ headers: await headers() })

as always, but this time throws

Image

Hi, Nextjs actually has multiple "use cache" directives. "use cache: private" is likely the one you want as actually you aren't allowed to use headers as they are from the client and cant be cached. Here is a link to the Nextjs docs about "use cache: private": https://nextjs.org/docs/app/api-reference/directives/use-cache-private. Having said this I am having a lot of issues too trying to get better auth to work with nextjs 16 although part of this is that server actions work strangely with cookies.

<!-- gh-comment-id:3465870568 --> @fringe4life commented on GitHub (Oct 30, 2025): > yup, i got errors when adding to next.config.ts cacheComponents: true > > and then trying to use: const session = await auth.api.getSession({ headers: await headers() }) > > as always, but this time throws > > <img alt="Image" width="1233" height="715" src="https://private-user-images.githubusercontent.com/72767265/507265537-288fb55b-ddd4-474d-ab91-f8ba040841f4.png?jwt=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3NjE3OTA3MzQsIm5iZiI6MTc2MTc5MDQzNCwicGF0aCI6Ii83Mjc2NzI2NS81MDcyNjU1MzctMjg4ZmI1NWItZGRkNC00NzRkLWFiOTEtZjhiYTA0MDg0MWY0LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTEwMzAlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUxMDMwVDAyMTM1NFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWNmNjZiMjUzM2M5NzI2ZjhiMTQxM2VkZGFhMGE0NTkxODI4NTJmNjUxZWFlZWM1ZDQxMTE2ZWM2OTFkZDBlY2ImWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.rv6ilrIJcMXyXAY3i6LBe6Imj5lOX4cbIHmax741CqI"> Hi, Nextjs actually has multiple "use cache" directives. ```"use cache: private"``` is likely the one you want as actually you aren't allowed to use headers as they are from the client and cant be cached. Here is a link to the Nextjs docs about ```"use cache: private"```: https://nextjs.org/docs/app/api-reference/directives/use-cache-private. Having said this I am having a lot of issues too trying to get better auth to work with nextjs 16 although part of this is that server actions work strangely with cookies.
Author
Owner

@better-auth-agent[bot] commented on GitHub (Oct 30, 2025):

It looks like the issue is due to stricter caching (and header reuse) rules in Next.js 16. When you enable component caching in your Next.js configuration, the headers (which are needed to fetch the session) are being cached unexpectedly. As a next step, try updating your caching directive to use a private cache (for example, using "use cache: private") so that client headers aren’t cached. This way, each request will contain fresh header data, which is required for auth.api.getSession to work correctly [1].

Also, verify that your Better Auth version includes the most recent changes for Next.js 16 integration. There have been updates clarifying middleware vs. proxy usage and how cookies/session data are handled with server actions. You might need to adjust middleware or proxy configuration accordingly [2].

In summary:

  1. Change your caching directive (in next.config.ts or in your route) to "use cache: private" to make sure headers aren’t cached.
  2. Check that you’re using an updated version of Better Auth with the Next.js 16 migration changes.
  3. Test the setup again, and if issues persist, try temporarily disabling caching for your session request endpoints to isolate the problem.

Let me know if you need further assistance!

DiagramDiscordGitHub

Diagram Join Star

<!-- gh-comment-id:3465871126 --> @better-auth-agent[bot] commented on GitHub (Oct 30, 2025): It looks like the issue is due to stricter caching (and header reuse) rules in Next.js 16. When you enable component caching in your Next.js configuration, the headers (which are needed to fetch the session) are being cached unexpectedly. As a next step, try updating your caching directive to use a private cache (for example, using "use cache: private") so that client headers aren’t cached. This way, each request will contain fresh header data, which is required for auth.api.getSession to work correctly [[1]](https://github.com/better-auth/better-auth/issues/5263). Also, verify that your Better Auth version includes the most recent changes for Next.js 16 integration. There have been updates clarifying middleware vs. proxy usage and how cookies/session data are handled with server actions. You might need to adjust middleware or proxy configuration accordingly [[2]](https://github.com/better-auth/better-auth/issues/5584). In summary: 1. Change your caching directive (in next.config.ts or in your route) to "use cache: private" to make sure headers aren’t cached. 2. Check that you’re using an updated version of Better Auth with the Next.js 16 migration changes. 3. Test the setup again, and if issues persist, try temporarily disabling caching for your session request endpoints to isolate the problem. Let me know if you need further assistance! <!-- bot:webhook reply v1 --> [Diagram](https://repodiagrams.s3.eu-north-1.amazonaws.com/better-auth_ultra_detailed_interactive.html) • [Discord](https://discord.gg/better-auth) • [GitHub](https://github.com/better-auth/better-auth) [![Diagram](https://img.shields.io/badge/Diagram-2b3137?style=flat-square)](https://repodiagrams.s3.eu-north-1.amazonaws.com/better-auth_ultra_detailed_interactive.html) [![Join](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&style=flat-square)](https://discord.gg/better-auth) [![Star](https://img.shields.io/badge/star-181717?logo=github&logoColor=white&style=flat-square)](https://github.com/better-auth/better-auth)
Author
Owner

@ping-maxwell commented on GitHub (Jan 8, 2026):

Hey we've fixed several bugs since Next 16 and no new issues specifically about it in this thread has appeared. For now I'm going to mark this as resolved. In order to keep this thread clean, please open new issues regarding Next 16 if this is still a valid issue, and please make the issue title is not broad but rather specific to your case.

<!-- gh-comment-id:3725108071 --> @ping-maxwell commented on GitHub (Jan 8, 2026): Hey we've fixed several bugs since Next 16 and no new issues specifically about it in this thread has appeared. For now I'm going to mark this as resolved. In order to keep this thread clean, please open new issues regarding Next 16 if this is still a valid issue, and please make the issue title is not broad but rather specific to your case.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#18828