fix(astro): Fix compatibility with Astro v6 Cloudflare adapter#7890
fix(astro): Fix compatibility with Astro v6 Cloudflare adapter#7890alexcarpenter wants to merge 3 commits intomainfrom
Conversation
Astro v6's Cloudflare adapter removed `locals.runtime.env`, which causes `@clerk/astro` to crash. This adds a fallback that imports env from `cloudflare:workers` when `locals.runtime.env` is unavailable, while maintaining backwards compatibility with Astro v4/v5. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
🦋 Changeset detectedLatest commit: 3c68b4d The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
The latest updates on your projects. Learn more about Vercel for GitHub. 1 Skipped Deployment
|
@clerk/agent-toolkit
@clerk/astro
@clerk/backend
@clerk/chrome-extension
@clerk/clerk-js
@clerk/dev-cli
@clerk/expo
@clerk/expo-passkeys
@clerk/express
@clerk/fastify
@clerk/hono
@clerk/localizations
@clerk/nextjs
@clerk/nuxt
@clerk/react
@clerk/react-router
@clerk/shared
@clerk/tanstack-react-start
@clerk/testing
@clerk/ui
@clerk/upgrade
@clerk/vue
commit: |
📝 WalkthroughWalkthroughAdds Astro v6 compatibility for the Cloudflare adapter: broadens the astro peerDependency to include ^6.0.0, makes Possibly related issues
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@packages/astro/src/server/get-safe-env.ts`:
- Around line 6-32: Add unit tests covering the Cloudflare env fallback: write
tests for initCloudflareEnv, the cached cloudflareEnv value, and
getContextEnvVar's fallback behavior when cloudflare:workers is present and when
it throws. In one test mock dynamic import of the moduleName
'cloudflare:workers' to return an object with env (e.g., {env: {KEY: "value"}}),
call initCloudflareEnv() and assert cloudflareEnv is set and
getContextEnvVar('KEY') returns the mocked value; in another test mock the
dynamic import to throw, call initCloudflareEnv(), assert cloudflareEnv becomes
null and getContextEnvVar('KEY') falls back to the non-Cloudflare path. Ensure
tests reset/clear the cloudflareEnv cache between cases so caching behavior is
validated.
| /** | ||
| * Cached env object from `cloudflare:workers` for Astro v6+ Cloudflare adapter. | ||
| * - `undefined`: not yet attempted | ||
| * - `null`: attempted but not available (non-Cloudflare environment) | ||
| * - object: the env object from `cloudflare:workers` | ||
| */ | ||
| let cloudflareEnv: Record<string, string> | null | undefined; | ||
|
|
||
| /** | ||
| * @internal | ||
| * Attempts to import env from `cloudflare:workers` and caches the result. | ||
| * This is needed for Astro v6+ where `locals.runtime.env` is no longer available. | ||
| * Safe to call in non-Cloudflare environments — will no-op. | ||
| */ | ||
| async function initCloudflareEnv(): Promise<void> { | ||
| if (cloudflareEnv !== undefined) { | ||
| return; | ||
| } | ||
| try { | ||
| // Use a variable to prevent TypeScript from resolving the module specifier | ||
| const moduleName = 'cloudflare:workers'; | ||
| const mod = await import(/* @vite-ignore */ moduleName); | ||
| cloudflareEnv = mod.env; | ||
| } catch { | ||
| cloudflareEnv = null; | ||
| } | ||
| } |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
# Check whether any tests were changed in this PR
git diff --name-only origin/main...HEAD | rg -i '(__tests__|test|spec)\.(ts|tsx|js|jsx)$'
# Search for existing tests covering the new Cloudflare env fallback path
rg -n 'initCloudflareEnv|getContextEnvVar|cloudflare:workers' \
-g '**/__tests__/**' -g '*.{test,spec}.{ts,tsx,js,jsx}' 2>/dev/null || trueRepository: clerk/javascript
Length of output: 100
🏁 Script executed:
#!/bin/bash
# Find the packages/astro directory and look for test files
find packages/astro -type f \( -name '*.test.ts' -o -name '*.test.tsx' -o -name '*.spec.ts' -o -name '*.spec.tsx' -o -path '*/__tests__/*' \) | head -20
# Also check the structure of the packages/astro directory
echo "=== packages/astro structure ==="
find packages/astro -type d | head -20
# Search across all test files for cloudflare-related tests
echo "=== Searching for cloudflare env tests ==="
rg 'cloudflare.*env|initCloudflareEnv' --type ts --type tsx 2>/dev/null || echo "No matches found"
# Look for getContextEnvVar or get-safe-env related tests
echo "=== Searching for get-safe-env tests ==="
rg 'getSafeEnv|getContextEnvVar|get-safe-env' --type ts --type tsx 2>/dev/null | head -20 || echo "No matches found"Repository: clerk/javascript
Length of output: 995
🏁 Script executed:
#!/bin/bash
# Read the complete get-safe-env.ts file
echo "=== packages/astro/src/server/get-safe-env.ts ==="
cat packages/astro/src/server/get-safe-env.ts
echo ""
echo "=== Checking if any test files import get-safe-env ==="
rg 'get-safe-env|getSafeEnv' packages/astro/src --type ts --type tsx
echo ""
echo "=== Contents of the existing test file ==="
cat packages/astro/src/internal/__tests__/create-clerk-instance.test.ts | head -100Repository: clerk/javascript
Length of output: 8030
Add tests for the Cloudflare env fallback logic.
The new initCloudflareEnv() function and the cloudflareEnv fallback path in getContextEnvVar() (lines 20–33 and 47–50) lack test coverage. Add tests to verify that the fallback correctly imports from cloudflare:workers in Cloudflare environments and gracefully handles non-Cloudflare environments.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@packages/astro/src/server/get-safe-env.ts` around lines 6 - 32, Add unit
tests covering the Cloudflare env fallback: write tests for initCloudflareEnv,
the cached cloudflareEnv value, and getContextEnvVar's fallback behavior when
cloudflare:workers is present and when it throws. In one test mock dynamic
import of the moduleName 'cloudflare:workers' to return an object with env
(e.g., {env: {KEY: "value"}}), call initCloudflareEnv() and assert cloudflareEnv
is set and getContextEnvVar('KEY') returns the mocked value; in another test
mock the dynamic import to throw, call initCloudflareEnv(), assert cloudflareEnv
becomes null and getContextEnvVar('KEY') falls back to the non-Cloudflare path.
Ensure tests reset/clear the cloudflareEnv cache between cases so caching
behavior is validated.
jacekradko
left a comment
There was a problem hiding this comment.
Some tests would be nice, but the functionality looks good
160b887 to
467fcc4
Compare
Astro v6's Cloudflare adapter removed
locals.runtime.env, which causes@clerk/astroto crash. This adds a fallback that imports env fromcloudflare:workerswhenlocals.runtime.envis unavailable, while maintaining backwards compatibility with Astro v4/v5.Description
fixes #7849
Checklist
pnpm testruns as expected.pnpm buildruns as expected.Type of change
Summary by CodeRabbit
New Features
Bug Fixes
Tests