Repository navigation
perf: reuse the auth instance when the database is process-wide - #659
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Latest result Pull request merged. Final answersSession Pull request merged. Session Waiting for GitHub merge request 9ae77912-9bf6-465c-bc08-1049f7fa54dc. Session Fixed Pullfrog P1: Nitro runtime Cloudflare bindings now prevent process-wide auth caching for Hyperdrive requests. The regression failed before the fix; all 38 focused tests and lint now pass. Pushed e7cb9f7 and resolved the review thread. Awaiting current-head CI. Session Fixed Pullfrog P1: Nitro runtime Cloudflare bindings now prevent process-wide auth caching for Hyperdrive requests. The regression failed before the fix; all 38 focused tests and lint now pass. Pushed e7cb9f7 and resolved the review thread. Awaiting current-head CI. Session Fixed Pullfrog P1: Nitro runtime Cloudflare bindings now prevent process-wide auth caching for Hyperdrive requests. The regression failed before the fix; all 38 focused tests and lint now pass. Pushed e7cb9f7 and resolved the review thread. Awaiting current-head CI. Session Fixed Pullfrog P1: Nitro runtime Cloudflare bindings now prevent process-wide auth caching for Hyperdrive requests. The regression failed before the fix; all 38 focused tests and lint now pass. Pushed e7cb9f7 and resolved the review thread. Awaiting current-head CI. Session Fixed Pullfrog P1: Nitro runtime Cloudflare bindings now prevent process-wide auth caching for Hyperdrive requests. The regression failed before the fix; all 38 focused tests and lint now pass. Pushed e7cb9f7 and resolved the review thread. Awaiting current-head CI. Session Fixed Pullfrog P1: Nitro runtime Cloudflare bindings now prevent process-wide auth caching for Hyperdrive requests. The regression failed before the fix; all 38 focused tests and lint now pass. Pushed e7cb9f7 and resolved the review thread. Awaiting current-head CI. Session Fixed Pullfrog P1: Nitro runtime Cloudflare bindings now prevent process-wide auth caching for Hyperdrive requests. The regression failed before the fix; all 38 focused tests and lint now pass. Pushed e7cb9f7 and resolved the review thread. Awaiting current-head CI. Session Fixed Pullfrog P1: Nitro runtime Cloudflare bindings now prevent process-wide auth caching for Hyperdrive requests. The regression failed before the fix; all 38 focused tests and lint now pass. Pushed e7cb9f7 and resolved the review thread. Awaiting current-head CI. |
commit: |
Deploying with
|
| Status | Name | Latest Commit | Preview URL | Updated (UTC) |
|---|---|---|---|---|
| ✅ Deployment successful! View logs |
better-auth | e7cb9f7 | Commit Preview URL Branch Preview URL |
Oct 26 2026, 05:40 AM |
There was a problem hiding this comment.
Important
The request-scope check misses Nitro's documented Cloudflare binding shape, so Hyperdrive-backed requests can still take the process-wide auth cache path.
Reviewed changes
- Generated database scope detection — Added
isRequestScopedDatabase(event)to distinguish Hyperdrive-backed database access from process-wide NuxtHub adapters. - Auth cache ordering — Moved the process-wide cache lookup before
createServerAuth()so cache hits do not rerun configuration and plugin factories. - Regression coverage and generated types — Updated virtual-module declarations and cache tests for process-wide, request-scoped, and database-less setups.
gpt-5.6-luna(high) | 𝕏
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
The incremental review covered the Hyperdrive binding lookup fix and its regression coverage since the prior Pullfrog review.
- Recognized Nitro runtime bindings — Added the
event.req.runtime.cloudflare.envlookup before process-wide fallbacks so request-bound Hyperdrive clients remain request-scoped. - Covered both binding locations — Added generated-database tests for
event.context.cloudflare.envand Nitro's runtime binding shape, plus the process-wide no-connection-string case.
gpt-5.6-luna(high) | 𝕏

With a NuxtHub database,
serverAuth()builds a new Better Auth instance on every request, and it calls the app'sdefineServerAuthfactory before checking the cache, so every plugin factory runs again too. Each SSR page hits this through the/api/auth/get-sessioncall.#295 made database-backed instances per-request because a Hyperdrive
postgresclient must not outlive a Worker request. That is the only request-scoped case: NuxtHub createsdbonce per process or isolate for its other drivers, D1 included.This PR:
isRequestScopedDatabase(event)to#auth/database(true only when a Hyperdrive binding with a connection string is present);createServerAuth(), so a cache hit skips the plugin factories.serverAuth()is documented as a module-level singleton, so this brings database setups in line with the docs.Repro
https://github.com/onmax/repros/tree/repro/better-auth-shared-db-cache/better-auth-shared-db-cache
The
-fixfixture applies this change as apnpm patchto 0.3.13.Effect
Production build of a Portal-shaped Nuxt 4.6.2 app (NuxtHub Postgres via postgres-js; jwt, mcp and cimd plugins). Server CPU per request, mean of 2 interleaved runs, identical HTML:
/(auth: { only: 'guest' })In the CPU profile,
serverAuthtook 2.0 ms of a 9.5 ms request: 1.6 ms in the plugin factories and 0.6 ms inbetterAuth()(schema index resolution, drizzle introspection and diff).Checks
test/server-auth-database-cache.test.ts: per-request tests now setisRequestScopedDatabaseto true; a new test checks one instance and one factory call across requests with a process-wide adapter. The new test and the updated no-database test fail onmain.pnpm lint,pnpm typecheck,pnpm typecheck:runtime-server,pnpm test:runtime, and the server-auth vitest files pass locally.🤖 Generated with Claude Code