Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-astro-cf-pages-env-fallback.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@clerk/astro": patch
---

Fix Cloudflare Pages compatibility by falling through to `locals.runtime.env` when `cloudflare:workers` env is missing the requested key
15 changes: 15 additions & 0 deletions packages/astro/src/server/__tests__/get-safe-env.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,21 @@ describe('get-safe-env', () => {
const env = getSafeEnv({ locals } as any);
expect(env.sk).toBe('sk_from_cf_workers');
});

it('falls back to locals.runtime.env when cloudflareEnv is missing the key (CF Pages)', async () => {
// On CF Pages, cloudflare:workers env may have bindings (D1, R2) but
// not dashboard secrets like CLERK_SECRET_KEY.
vi.doMock('cloudflare:workers', () => ({
env: { SOME_OTHER_BINDING: 'value' },
}));

const { initCloudflareEnv, getSafeEnv } = await import('../get-safe-env');
await initCloudflareEnv();

const locals = { runtime: { env: { CLERK_SECRET_KEY: 'sk_from_runtime' } } };
const env = getSafeEnv({ locals } as any);
expect(env.sk).toBe('sk_from_runtime');
});
});
});

Expand Down
7 changes: 6 additions & 1 deletion packages/astro/src/server/get-safe-env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,13 @@ function getContextEnvVar(envVarName: keyof InternalEnv, contextOrLocals: Contex
// Astro v6+ Cloudflare adapter: env from cloudflare:workers
// Checked first to avoid the expensive try/catch on locals.runtime.env,
// which throws on every call in Astro v6 Cloudflare environments.
// Falls through when the key is missing — on CF Pages (Astro v5),
// cloudflare:workers env may not include dashboard secrets.
if (cloudflareEnv) {
return cloudflareEnv[envVarName];
const value = cloudflareEnv[envVarName];
if (value !== undefined) {
return value;
}
}

// Astro v4/v5 Cloudflare adapter: env is on locals.runtime.env
Expand Down
Loading