Skip to content

Commit eec0c0f

Browse files
mlafeldtjescalan
authored andcommitted
fix(astro): Restore Cloudflare Pages compatibility broken by cloudflare:workers env
On Cloudflare Pages (Astro v5), `cloudflare:workers` env may be available but lack dashboard secrets (CLERK_SECRET_KEY, etc.) that are only present in the fetch handler's env param (locals.runtime.env). The current code short-circuits on cloudflareEnv without checking whether the requested key actually exists, preventing the fallback chain from ever reaching locals.runtime.env. This causes all SSR pages to break with `[object Object]` responses. Fix: check if the value is defined before returning from the cloudflareEnv branch, allowing the fallback to locals.runtime.env.
1 parent ad782ff commit eec0c0f

3 files changed

Lines changed: 26 additions & 1 deletion

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@clerk/astro": patch
3+
---
4+
5+
Fix Cloudflare Pages compatibility by falling through to `locals.runtime.env` when `cloudflare:workers` env is missing the requested key

packages/astro/src/server/__tests__/get-safe-env.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,21 @@ describe('get-safe-env', () => {
136136
const env = getSafeEnv({ locals } as any);
137137
expect(env.sk).toBe('sk_from_cf_workers');
138138
});
139+
140+
it('falls back to locals.runtime.env when cloudflareEnv is missing the key (CF Pages)', async () => {
141+
// On CF Pages, cloudflare:workers env may have bindings (D1, R2) but
142+
// not dashboard secrets like CLERK_SECRET_KEY.
143+
vi.doMock('cloudflare:workers', () => ({
144+
env: { SOME_OTHER_BINDING: 'value' },
145+
}));
146+
147+
const { initCloudflareEnv, getSafeEnv } = await import('../get-safe-env');
148+
await initCloudflareEnv();
149+
150+
const locals = { runtime: { env: { CLERK_SECRET_KEY: 'sk_from_runtime' } } };
151+
const env = getSafeEnv({ locals } as any);
152+
expect(env.sk).toBe('sk_from_runtime');
153+
});
139154
});
140155
});
141156

packages/astro/src/server/get-safe-env.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,13 @@ function getContextEnvVar(envVarName: keyof InternalEnv, contextOrLocals: Contex
4141
// Astro v6+ Cloudflare adapter: env from cloudflare:workers
4242
// Checked first to avoid the expensive try/catch on locals.runtime.env,
4343
// which throws on every call in Astro v6 Cloudflare environments.
44+
// Falls through when the key is missing — on CF Pages (Astro v5),
45+
// cloudflare:workers env may not include dashboard secrets.
4446
if (cloudflareEnv) {
45-
return cloudflareEnv[envVarName];
47+
const value = cloudflareEnv[envVarName];
48+
if (value !== undefined) {
49+
return value;
50+
}
4651
}
4752

4853
// Astro v4/v5 Cloudflare adapter: env is on locals.runtime.env

0 commit comments

Comments
 (0)