Skip to content

perf: reuse the auth instance when the database is process-wide - #659

Merged
vite-hub[bot] merged 2 commits into
mainfrom
perf/cache-auth-for-shared-database
Oct 26, 2026
Merged

vite-hub[bot] merged 2 commits into
mainfrom
perf/cache-auth-for-shared-database

Conversation

@onmax

@onmax onmax commented Oct 26, 2026

Copy link
Copy Markdown
Collaborator

With a NuxtHub database, serverAuth() builds a new Better Auth instance on every request, and it calls the app's defineServerAuth factory before checking the cache, so every plugin factory runs again too. Each SSR page hits this through the /api/auth/get-session call.

#295 made database-backed instances per-request because a Hyperdrive postgres client must not outlive a Worker request. That is the only request-scoped case: NuxtHub creates db once per process or isolate for its other drivers, D1 included.

This PR:

  • adds isRequestScopedDatabase(event) to #auth/database (true only when a Hyperdrive binding with a connection string is present);
  • caches the instance unless the database is request-scoped (the derived-secret rule is unchanged);
  • looks the cache up before calling 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

git clone --branch repro/better-auth-shared-db-cache https://github.com/onmax/repros
cd repros && node verify.mjs
better-auth-shared-db-cache: 10 page requests -> defineServerAuth factory called 10 times (expected 10)
better-auth-shared-db-cache: serverAuth() returns the same instance on the next request: false (expected false)
better-auth-shared-db-cache-fix: 10 page requests -> defineServerAuth factory called 1 times (expected 1)
better-auth-shared-db-cache-fix: serverAuth() returns the same instance on the next request: true (expected true)

The -fix fixture applies this change as a pnpm patch to 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:

page before after
near-empty page 15.4 ms 9.7 ms (−37%)
form, ~40 fields 59.0 ms 53.1 ms (−10%)
guest / (auth: { only: 'guest' }) 34.4 ms 20.3 ms (−41%)

In the CPU profile, serverAuth took 2.0 ms of a 9.5 ms request: 1.6 ms in the plugin factories and 0.6 ms in betterAuth() (schema index resolution, drizzle introspection and diff).

Checks

  • test/server-auth-database-cache.test.ts: per-request tests now set isRequestScopedDatabase to 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 on main.
  • pnpm lint, pnpm typecheck, pnpm typecheck:runtime-server, pnpm test:runtime, and the server-auth vitest files pass locally.

🤖 Generated with Claude Code

@vercel

vercel Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
nuxt-better-auth-docs Ready Ready Preview Oct 26, 2026 5:40am UTC

@vite-hub

vite-hub Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor
Session Status Started Duration
Pending Completed Not started —

Latest result

Pull request merged.

Final answers

Session

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.

@pkg-pr-new

pkg-pr-new Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@nuxtjs/better-auth@659

commit: e7cb9f7

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

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

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using gpt-5.6-luna(high) | 𝕏

Comment thread src/module/templates.ts Resolved

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ 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.env lookup before process-wide fallbacks so request-bound Hyperdrive clients remain request-scoped.
  • Covered both binding locations — Added generated-database tests for event.context.cloudflare.env and Nitro's runtime binding shape, plus the process-wide no-connection-string case.

Pullfrog  | View workflow run | Using gpt-5.6-luna(high) | 𝕏

@vite-hub
vite-hub Bot merged commit 44f1ead into main Oct 26, 2026
12 checks passed
@github-actions github-actions Bot mentioned this pull request Oct 26, 2026

This branch was successfully deployed

1 active deployment
Preview — e7cb9f78 Deployed Oct 26, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant