Skip to content

Comments

fix(astro): Fall back to process.env for runtime environment variables#7889

Open
alexcarpenter wants to merge 4 commits intomainfrom
ac/astro-process-env-fallback
Open

fix(astro): Fall back to process.env for runtime environment variables#7889
alexcarpenter wants to merge 4 commits intomainfrom
ac/astro-process-env-fallback

Conversation

@alexcarpenter
Copy link
Contributor

@alexcarpenter alexcarpenter commented Feb 19, 2026

Add process.env as a third fallback in getContextEnvVar() so that PUBLIC_CLERK_PUBLISHABLE_KEY and other env vars set at runtime (e.g., via the Astro Node adapter) are properly read when import.meta.env.PUBLIC_* is statically replaced at build time by Vite.

Description

fixes #7841

Checklist

  • pnpm test runs as expected.
  • pnpm build runs as expected.
  • (If applicable) JSDoc comments have been added or updated for any package exports
  • (If applicable) Documentation has been updated

Type of change

  • 🐛 Bug fix
  • 🌟 New feature
  • 🔨 Breaking change
  • 📖 Refactoring / dependency upgrade / documentation
  • other:

Summary by CodeRabbit

  • Bug Fixes

    • Fixes runtime reading of the publishable key when build-time env replacements aren’t available by adding a fallback to process.env.
  • Tests

    • Adds comprehensive tests for environment variable precedence and fallback behavior across different runtime environments.

Add process.env as a third fallback in getContextEnvVar() so that
PUBLIC_CLERK_PUBLISHABLE_KEY and other env vars set at runtime
(e.g., via the Astro Node adapter) are properly read when
import.meta.env.PUBLIC_* is statically replaced at build time by Vite.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@vercel
Copy link

vercel bot commented Feb 19, 2026

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

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
clerk-js-sandbox Skipped Skipped Feb 19, 2026 8:28pm

Request Review

@changeset-bot
Copy link

changeset-bot bot commented Feb 19, 2026

🦋 Changeset detected

Latest commit: 16d0346

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@clerk/astro Patch

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

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 19, 2026

📝 Walkthrough

Walkthrough

The PR adds a runtime fallback in the Astro integration's getContextEnvVar(): it reads import.meta.env first and, if that value is missing, falls back to process.env; returns undefined if neither is present. Tests for getSafeEnv and getClientSafeEnv were added to verify source precedence, keylessPublishableKey precedence, and fallback behavior for publishable and public env vars across runtime contexts.

Possibly related issues

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: adding a process.env fallback mechanism for runtime environment variables in the Astro module, which is the primary objective of the PR.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Comment @coderabbitai help to get the list of available commands and usage tips.

alexcarpenter and others added 2 commits February 19, 2026 15:07
Add process.env as a third fallback in getContextEnvVar() so that
PUBLIC_CLERK_PUBLISHABLE_KEY and other env vars set at runtime
(e.g., via the Astro Node adapter) are properly read when
import.meta.env.PUBLIC_* is statically replaced at build time by Vite.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…script into ac/astro-process-env-fallback

# Conflicts:
#	packages/astro/src/server/__tests__/get-safe-env.test.ts
Copy link
Member

@wobsoriano wobsoriano left a comment

Choose a reason for hiding this comment

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

thanks!

Use `as unknown as App.Locals` cast in createLocals to avoid strict
assignability errors from Partial<App.Locals> spreading.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@pkg-pr-new
Copy link

pkg-pr-new bot commented Feb 19, 2026

Open in StackBlitz

@clerk/agent-toolkit

npm i https://pkg.pr.new/@clerk/agent-toolkit@7889

@clerk/astro

npm i https://pkg.pr.new/@clerk/astro@7889

@clerk/backend

npm i https://pkg.pr.new/@clerk/backend@7889

@clerk/chrome-extension

npm i https://pkg.pr.new/@clerk/chrome-extension@7889

@clerk/clerk-js

npm i https://pkg.pr.new/@clerk/clerk-js@7889

@clerk/dev-cli

npm i https://pkg.pr.new/@clerk/dev-cli@7889

@clerk/expo

npm i https://pkg.pr.new/@clerk/expo@7889

@clerk/expo-passkeys

npm i https://pkg.pr.new/@clerk/expo-passkeys@7889

@clerk/express

npm i https://pkg.pr.new/@clerk/express@7889

@clerk/fastify

npm i https://pkg.pr.new/@clerk/fastify@7889

@clerk/hono

npm i https://pkg.pr.new/@clerk/hono@7889

@clerk/localizations

npm i https://pkg.pr.new/@clerk/localizations@7889

@clerk/nextjs

npm i https://pkg.pr.new/@clerk/nextjs@7889

@clerk/nuxt

npm i https://pkg.pr.new/@clerk/nuxt@7889

@clerk/react

npm i https://pkg.pr.new/@clerk/react@7889

@clerk/react-router

npm i https://pkg.pr.new/@clerk/react-router@7889

@clerk/shared

npm i https://pkg.pr.new/@clerk/shared@7889

@clerk/tanstack-react-start

npm i https://pkg.pr.new/@clerk/tanstack-react-start@7889

@clerk/testing

npm i https://pkg.pr.new/@clerk/testing@7889

@clerk/ui

npm i https://pkg.pr.new/@clerk/ui@7889

@clerk/upgrade

npm i https://pkg.pr.new/@clerk/upgrade@7889

@clerk/vue

npm i https://pkg.pr.new/@clerk/vue@7889

commit: 16d0346

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Astro: PUBLIC_CLERK_PUBLISHABLE_KEY cannot be changed after build

2 participants