[bug] Better Auth addon generates legacy D1 binding access with SvelteKit 3 and Cloudflare adapter 8
メンテナーはふだん 4 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 初心者へのやさしさ
- 82/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 技術スタック
- typescript
調査の方向性
Cloudflare Workers と Drizzle D1 向けに src/hooks.server.ts を生成する Better Auth アドオンのテンプレートを見つけてください。まず CLI テンプレートで event.platform.env.DB を検索し、ほかの Cloudflare テンプレートがどのようにバインディングにアクセスしているか確認してください。生成されるフックを、サポートされているバインディング API を使うように更新し、生成したプロジェクトが起動して、そのルートが HTTP 200 を返すことを確認してください。
索引モデルが issue の本文から書いたものです。
説明
Describe the bug
When creating a new SvelteKit project using sv create with the Cloudflare Workers adapter, Drizzle (Cloudflare D1), and Better Auth (Email & Password) addons, the generated src/hooks.server.ts uses the legacy event.platform.env.DB API to access the D1 database binding.
With SvelteKit 3 and @sveltejs/adapter-cloudflare 8, Cloudflare-specific bindings are no longer exposed through event.platform.
As a result, the generated application fails with HTTP 500 errors during local development, even when the D1 binding is correctly configured.
Replacing event.platform.env.DB with env.DB imported from cloudflare:workers resolves the issue.
Steps to reproduce
-
Run:
npx [email protected] create -
Select the following options:
- Template:
SvelteKit minimal - TypeScript:
Yes - Adapter:
Cloudflare→Workers - Drizzle:
Cloudflare D1 - Better Auth:
Email & Password
- Template:
-
Configure the Cloudflare D1 binding and environment variables as instructed by the CLI.
-
Start the development server:
npm run dev -
Navigate to
http://localhost:5173/.
Actual behavior
The generated src/hooks.server.ts contains:
const handleBetterAuth: Handle = async ({ event, resolve }) => {
if (!event.platform?.env?.DB)
throw new Error('D1 binding "DB" not found - are you running with wrangler?');
event.locals.auth = createAuth(event.platform.env.DB);
// ...
};
The application throws the following runtime error:
Error: D1 binding "DB" not found - are you running with wrangler?
at handleBetterAuth (src/hooks.server.ts:23:9)
500 GET /
500 GET /favicon.ico
Expected behavior
The generated Better Auth integration should use the supported Cloudflare binding access API for SvelteKit 3 and @sveltejs/adapter-cloudflare 8.
A correctly configured project should start successfully without requiring manual modifications to the generated authentication hook.
Suggested fix
Import env directly from cloudflare:workers instead of accessing bindings through event.platform.env.
+ import { env } from 'cloudflare:workers';
const handleBetterAuth: Handle = async ({ event, resolve }) => {
- if (!event.platform?.env?.DB)
+ if (!env?.DB)
throw new Error('D1 binding "DB" not found - are you running with wrangler?');
- event.locals.auth = createAuth(event.platform.env.DB);
+ event.locals.auth = createAuth(env.DB);
// Remaining handler unchanged
};
After applying this change, the application successfully started, and the previously failing routes returned HTTP 200 instead of HTTP 500.
Environment
| Package | Version |
|---|---|
sv |
1.2.0 |
@sveltejs/kit |
3.1.1 |
@sveltejs/adapter-cloudflare |
8.0.2 |
better-auth |
1.7.16 |
| Database | Cloudflare D1 |
| ORM | Drizzle |
| Package manager | npm |
| OS | Windows (PowerShell) |
Additional context
The SvelteKit 3 migration guide documents the removal of Cloudflare-specific APIs from event.platform and recommends using imports from cloudflare:workers instead.
This appears to be a compatibility issue in the Svelte CLI Better Auth addon template when generating projects targeting SvelteKit 3 and Cloudflare Workers.
The addon template should be updated to generate the appropriate Cloudflare binding access code.
Note: The initial setup also reported a separate Drizzle schema mismatch involving missing Better Auth tables (user, session, account, and verification). This report specifically concerns the D1 binding access issue, which was resolved by the change described above.
- 主要言語
- TypeScript
- スター
- 513
- フォーク
- 110
- 平均マージ
- 5日 1時間
- マージ済み PR(30日)
- 24
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
sveltejs/cli のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 4 日以内に返信
-
pkg:add
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
sveltejs/cli#1404 · コメント 1 件 ·
メンテナーはふだん 4 日以内に返信
-
pkg:add
難易度 1/5 1時間未満 初心者へのやさしさ 78/100
sveltejs/cli#1399 · コメント 2 件 ·
メンテナーはふだん 4 日以内に返信
-
pkg:migrate
難易度 3/5 1〜2日 初心者へのやさしさ 35/100
sveltejs/cli#1461 · コメント 2 件 ·
メンテナーはふだん 4 日以内に返信
-
enhancement pkg:migrate
難易度 3/5 1〜2日 初心者へのやさしさ 53/100
sveltejs/cli#1440 · コメント 3 件 · リアクション 1 件 ·
メンテナーはふだん 4 日以内に返信
似ている issue
-
[analytics-core] getQueryParams does not decode `+` as space and truncates values containing `=`オープンbug
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
amplitude/Amplitude-TypeScript#2179 · コメント 1 件 ·
メンテナーはふだん 2 日以内に返信
-
ci good first issue severity: low
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
TMHSDigital/GH-Arcade#111 ·
-
AsnIntegerConverter.toASN coerces strings with +value, rounding integers outside safe-integer rangeオープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
PeculiarVentures/asn1-schema#189 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
anaisbetts/spawn-rx#38 ·