[GH-ISSUE #5376] Docs recommend getCookieCache(), which is not compatible with NextJS edge environment #10223

Closed
opened 2026-04-13 06:12:35 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @michidk on GitHub (Oct 17, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/5376

Note

This issue replaces https://github.com/better-auth/better-auth/issues/5120, which was closed and not reopened, even though the requested proof and steps to reproduce were provided. This serves as new issue instead.

To Reproduce

  1. Implement the middleware mentioned here using getSessionCookie() or getCookieCache(): https://www.better-auth.com/docs/integrations/next#middleware
import { NextRequest, NextResponse } from "next/server";
import { getSessionCookie } from "better-auth/cookies";

export async function middleware(request: NextRequest) {
	const sessionCookie = getSessionCookie(request);

    // THIS IS NOT SECURE!
    // This is the recommended approach to optimistically redirect users
    // We recommend handling auth checks in each page/route
	if (!sessionCookie) {
		return NextResponse.redirect(new URL("/", request.url));
	}

	return NextResponse.next();
}

export const config = {
	matcher: ["/dashboard"], // Specify the routes the middleware applies to
};

or

import { getCookieCache } from "better-auth/cookies";

export async function middleware(request: NextRequest) {
	const session = await getCookieCache(request);
	if (!session) {
		return NextResponse.redirect(new URL("/sign-in", request.url));
	}
	return NextResponse.next();
}
  1. Build the project
$ next build
   ▲ Next.js 15.3.3
   
Environments: .env.local

   Creating an optimized production build ...
 ⚠ Compiled with warnings in 1000ms

./node_modules/better-auth/dist/shared/better-auth.DgGir396.mjs
A Node.js API is used (process.platform at line: 74) which is not supported in the Edge Runtime.
Learn more: https://nextjs.org/docs/api-reference/edge-runtime

Import trace for requested module:
./node_modules/better-auth/dist/shared/better-auth.DgGir396.mjs
./node_modules/better-auth/dist/cookies/index.mjs

 ✓ Compiled successfully in 55s

Current vs. Expected behavior

NextJS builds without warnings and the middleware works

What version of Better Auth are you using?

1.3.24

System info

{
  "system": {
    "platform": "linux",
    "arch": "x64",
    "version": "#1 SMP PREEMPT_DYNAMIC Thu Jun  5 18:30:46 UTC 2025",
    "release": "6.6.87.2-microsoft-standard-WSL2",
    "cpuCount": 16,
    "cpuModel": "12th Gen Intel(R) Core(TM) i5-12500H",
    "totalMemory": "15.46 GB",
    "freeMemory": "9.07 GB"
  },
  "node": {
    "version": "v22.15.0",
    "env": "development"
  },
  "packageManager": {
    "name": "bun",
    "version": "1.2.13"
  },
  "frameworks": [
    {
      "name": "next",
      "version": "15.3.3"
    },
    {
      "name": "react",
      "version": "19.1.0"
    }
  ],
  "databases": [
    {
      "name": "drizzle",
      "version": "0.44.2"
    },
    {
      "name": "@neondatabase/serverless",
      "version": "1.0.1"
    }
  ],
  "betterAuth": {
    "version": "^1.3.24",
    "config": null
  }
}

Which area(s) are affected? (Select all that apply)

Documentation, Client

Auth config (if applicable)

import { betterAuth } from "better-auth"
export const auth = betterAuth({
  emailAndPassword: {  
    enabled: true
  },
});

Additional context

here is a minimal repo:
https://github.com/michidk/better-auth-minimal

To create this I just executed: bun create next-app my-next-app , bun add better-auth and added the middleware from the docs.

Now run bun run build:

node ➜ /workspaces/better-auth-minimal $ bun run build
$ next build
   ▲ Next.js 15.5.4

   Creating an optimized production build ...
 ⚠ Compiled with warnings in 1422ms

./node_modules/better-auth/dist/shared/better-auth.DgGir396.mjs
A Node.js API is used (process.platform at line: 74) which is not supported in the Edge Runtime.
Learn more: https://nextjs.org/docs/api-reference/edge-runtime

Import trace for requested module:
./node_modules/better-auth/dist/shared/better-auth.DgGir396.mjs
./node_modules/better-auth/dist/cookies/index.mjs

 ✓ Compiled successfully in 7.4s
 ✓ Linting and checking validity of types    
 ✓ Collecting page data    
 ✓ Generating static pages (5/5)
 ✓ Collecting build traces    
 ✓ Finalizing page optimization    
Originally created by @michidk on GitHub (Oct 17, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/5376 > [!NOTE] > This issue replaces https://github.com/better-auth/better-auth/issues/5120, which was closed and not reopened, even though the requested proof and steps to reproduce were provided. This serves as new issue instead. ### To Reproduce 1. Implement the middleware mentioned here using `getSessionCookie()` or `getCookieCache()`: https://www.better-auth.com/docs/integrations/next#middleware ```typescript import { NextRequest, NextResponse } from "next/server"; import { getSessionCookie } from "better-auth/cookies"; export async function middleware(request: NextRequest) { const sessionCookie = getSessionCookie(request); // THIS IS NOT SECURE! // This is the recommended approach to optimistically redirect users // We recommend handling auth checks in each page/route if (!sessionCookie) { return NextResponse.redirect(new URL("/", request.url)); } return NextResponse.next(); } export const config = { matcher: ["/dashboard"], // Specify the routes the middleware applies to }; ``` or ```typescript import { getCookieCache } from "better-auth/cookies"; export async function middleware(request: NextRequest) { const session = await getCookieCache(request); if (!session) { return NextResponse.redirect(new URL("/sign-in", request.url)); } return NextResponse.next(); } ``` 2. Build the project ``` $ next build ▲ Next.js 15.3.3 Environments: .env.local Creating an optimized production build ... ⚠ Compiled with warnings in 1000ms ./node_modules/better-auth/dist/shared/better-auth.DgGir396.mjs A Node.js API is used (process.platform at line: 74) which is not supported in the Edge Runtime. Learn more: https://nextjs.org/docs/api-reference/edge-runtime Import trace for requested module: ./node_modules/better-auth/dist/shared/better-auth.DgGir396.mjs ./node_modules/better-auth/dist/cookies/index.mjs ✓ Compiled successfully in 55s ``` ### Current vs. Expected behavior NextJS builds without warnings and the middleware works ### What version of Better Auth are you using? 1.3.24 ### System info ```bash { "system": { "platform": "linux", "arch": "x64", "version": "#1 SMP PREEMPT_DYNAMIC Thu Jun 5 18:30:46 UTC 2025", "release": "6.6.87.2-microsoft-standard-WSL2", "cpuCount": 16, "cpuModel": "12th Gen Intel(R) Core(TM) i5-12500H", "totalMemory": "15.46 GB", "freeMemory": "9.07 GB" }, "node": { "version": "v22.15.0", "env": "development" }, "packageManager": { "name": "bun", "version": "1.2.13" }, "frameworks": [ { "name": "next", "version": "15.3.3" }, { "name": "react", "version": "19.1.0" } ], "databases": [ { "name": "drizzle", "version": "0.44.2" }, { "name": "@neondatabase/serverless", "version": "1.0.1" } ], "betterAuth": { "version": "^1.3.24", "config": null } } ``` ### Which area(s) are affected? (Select all that apply) Documentation, Client ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" export const auth = betterAuth({ emailAndPassword: { enabled: true }, }); ``` ### Additional context here is a minimal repo: https://github.com/michidk/better-auth-minimal To create this I just executed: `bun create next-app my-next-app ` , `bun add better-auth` and added the middleware from the docs. Now run `bun run build`: ``` node ➜ /workspaces/better-auth-minimal $ bun run build $ next build ▲ Next.js 15.5.4 Creating an optimized production build ... ⚠ Compiled with warnings in 1422ms ./node_modules/better-auth/dist/shared/better-auth.DgGir396.mjs A Node.js API is used (process.platform at line: 74) which is not supported in the Edge Runtime. Learn more: https://nextjs.org/docs/api-reference/edge-runtime Import trace for requested module: ./node_modules/better-auth/dist/shared/better-auth.DgGir396.mjs ./node_modules/better-auth/dist/cookies/index.mjs ✓ Compiled successfully in 7.4s ✓ Linting and checking validity of types ✓ Collecting page data ✓ Generating static pages (5/5) ✓ Collecting build traces ✓ Finalizing page optimization ```
GiteaMirror added the locked label 2026-04-13 06:12:35 -05:00
Author
Owner

@liby commented on GitHub (Nov 28, 2025):

I'm experiencing a similar but different issue with better-auth@1.4.3 and Next.js 14.2.4.

When using getSessionCookie() from better-auth/cookies in middleware, the build fails with:

Dynamic Code Evaluation (e. g. 'eval', 'new Function', 'WebAssembly.compile') not allowed in Edge Runtime

The error was caused by importing 'better-auth/dist/cookies/index.mjs' in './middleware.ts'.

The documentation recommends using getSessionCookie() for Edge Runtime middleware, but it doesn't work due to dynamic code evaluation in the module.

Workaround: Manually read cookies instead:

const sessionCookie = request.cookies.get('better-auth.session_token')
  || request.cookies.get('__Secure-better-auth.session_token');

Since this issue is closed, should I open a new issue to track this? It seems like the better-auth/cookies module still has Edge Runtime compatibility issues beyond the process.platform fix in PR #5390.

Thanks for the great library!

<!-- gh-comment-id:3589327110 --> @liby commented on GitHub (Nov 28, 2025): I'm experiencing a similar but different issue with `better-auth@1.4.3` and Next.js 14.2.4. When using `getSessionCookie()` from `better-auth/cookies` in middleware, the build fails with: ``` Dynamic Code Evaluation (e. g. 'eval', 'new Function', 'WebAssembly.compile') not allowed in Edge Runtime The error was caused by importing 'better-auth/dist/cookies/index.mjs' in './middleware.ts'. ``` The documentation recommends using `getSessionCookie()` for Edge Runtime middleware, but it doesn't work due to dynamic code evaluation in the module. **Workaround**: Manually read cookies instead: ```typescript const sessionCookie = request.cookies.get('better-auth.session_token') || request.cookies.get('__Secure-better-auth.session_token'); ``` Since this issue is closed, should I open a new issue to track this? It seems like the `better-auth/cookies` module still has Edge Runtime compatibility issues beyond the `process.platform` fix in PR #5390. Thanks for the great library\!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#10223