-
Notifications
You must be signed in to change notification settings - Fork 435
fix(astro): Fall back to process.env for runtime environment variables #7889
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
alexcarpenter
wants to merge
4
commits into
main
Choose a base branch
from
ac/astro-process-env-fallback
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e23e71a
fix(astro): Fall back to process.env for runtime environment variables
alexcarpenter cb96d9a
fix(astro): Fall back to process.env for runtime environment variables
alexcarpenter be7a4ff
Merge branch 'ac/astro-process-env-fallback' of github.com:clerk/java…
alexcarpenter 16d0346
fix(astro): Fix type error in get-safe-env test helper
alexcarpenter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@clerk/astro': patch | ||
| --- | ||
|
|
||
| Fix `PUBLIC_CLERK_PUBLISHABLE_KEY` not readable from runtime environment when using the Astro Node adapter. Added `process.env` as a fallback in `getContextEnvVar()` for cases where `import.meta.env.PUBLIC_*` is statically replaced at build time by Vite. |
133 changes: 133 additions & 0 deletions
133
packages/astro/src/server/__tests__/get-safe-env.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import { getClientSafeEnv, getSafeEnv } from '../get-safe-env'; | ||
|
|
||
| function createLocals(overrides: Partial<App.Locals> = {}): App.Locals { | ||
| return { | ||
| runtime: { env: {} as InternalEnv }, | ||
| ...overrides, | ||
| } as unknown as App.Locals; | ||
| } | ||
|
|
||
| describe('getSafeEnv', () => { | ||
| beforeEach(() => { | ||
| vi.stubEnv('PUBLIC_CLERK_PUBLISHABLE_KEY', ''); | ||
| vi.stubEnv('CLERK_SECRET_KEY', ''); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.unstubAllEnvs(); | ||
| }); | ||
|
|
||
| it('reads from locals.runtime.env first (Cloudflare)', () => { | ||
| const locals = createLocals({ | ||
| runtime: { | ||
| env: { | ||
| PUBLIC_CLERK_PUBLISHABLE_KEY: 'pk_from_runtime', | ||
| CLERK_SECRET_KEY: 'sk_from_runtime', | ||
| } as InternalEnv, | ||
| }, | ||
| }); | ||
|
|
||
| // Also set process.env to verify runtime.env takes priority | ||
| process.env.PUBLIC_CLERK_PUBLISHABLE_KEY = 'pk_from_process'; | ||
| process.env.CLERK_SECRET_KEY = 'sk_from_process'; | ||
|
|
||
| const env = getSafeEnv(locals); | ||
|
|
||
| expect(env.pk).toBe('pk_from_runtime'); | ||
| expect(env.sk).toBe('sk_from_runtime'); | ||
|
|
||
| delete process.env.PUBLIC_CLERK_PUBLISHABLE_KEY; | ||
| delete process.env.CLERK_SECRET_KEY; | ||
| }); | ||
|
|
||
| it('reads from import.meta.env when runtime.env is not available', () => { | ||
| vi.stubEnv('PUBLIC_CLERK_PUBLISHABLE_KEY', 'pk_from_meta'); | ||
| vi.stubEnv('CLERK_SECRET_KEY', 'sk_from_meta'); | ||
|
|
||
| const locals = createLocals({ runtime: { env: undefined as unknown as InternalEnv } }); | ||
| const env = getSafeEnv(locals); | ||
|
|
||
| expect(env.pk).toBe('pk_from_meta'); | ||
| expect(env.sk).toBe('sk_from_meta'); | ||
| }); | ||
|
|
||
| it('falls back to process.env when import.meta.env has no value', () => { | ||
| process.env.PUBLIC_CLERK_PUBLISHABLE_KEY = 'pk_from_process'; | ||
| process.env.CLERK_SECRET_KEY = 'sk_from_process'; | ||
|
|
||
| const locals = createLocals({ runtime: { env: undefined as unknown as InternalEnv } }); | ||
| const env = getSafeEnv(locals); | ||
|
|
||
| expect(env.pk).toBe('pk_from_process'); | ||
| expect(env.sk).toBe('sk_from_process'); | ||
|
|
||
| delete process.env.PUBLIC_CLERK_PUBLISHABLE_KEY; | ||
| delete process.env.CLERK_SECRET_KEY; | ||
| }); | ||
|
|
||
| it('returns undefined when no env source has the value', () => { | ||
| // Clean process.env so the fallback finds nothing | ||
| delete process.env.PUBLIC_CLERK_PUBLISHABLE_KEY; | ||
| delete process.env.CLERK_SECRET_KEY; | ||
|
|
||
| const locals = createLocals({ runtime: { env: undefined as unknown as InternalEnv } }); | ||
| const env = getSafeEnv(locals); | ||
|
|
||
| expect(env.pk).toBeUndefined(); | ||
| expect(env.sk).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('prefers keylessPublishableKey over all env sources', () => { | ||
| process.env.PUBLIC_CLERK_PUBLISHABLE_KEY = 'pk_from_process'; | ||
|
|
||
| const locals = createLocals({ | ||
| runtime: { env: undefined as unknown as InternalEnv }, | ||
| keylessPublishableKey: 'pk_keyless', | ||
| }); | ||
| const env = getSafeEnv(locals); | ||
|
|
||
| expect(env.pk).toBe('pk_keyless'); | ||
|
|
||
| delete process.env.PUBLIC_CLERK_PUBLISHABLE_KEY; | ||
| }); | ||
| }); | ||
|
|
||
| describe('getClientSafeEnv', () => { | ||
| beforeEach(() => { | ||
| vi.stubEnv('PUBLIC_CLERK_PUBLISHABLE_KEY', ''); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.unstubAllEnvs(); | ||
| }); | ||
|
|
||
| it('falls back to process.env for publishableKey', () => { | ||
| process.env.PUBLIC_CLERK_PUBLISHABLE_KEY = 'pk_from_process'; | ||
|
|
||
| const locals = createLocals({ runtime: { env: undefined as unknown as InternalEnv } }); | ||
| const env = getClientSafeEnv(locals); | ||
|
|
||
| expect(env.publishableKey).toBe('pk_from_process'); | ||
|
|
||
| delete process.env.PUBLIC_CLERK_PUBLISHABLE_KEY; | ||
| }); | ||
|
|
||
| it('falls back to process.env for all public env vars', () => { | ||
| process.env.PUBLIC_CLERK_DOMAIN = 'test.domain.com'; | ||
| process.env.PUBLIC_CLERK_SIGN_IN_URL = '/sign-in'; | ||
| process.env.PUBLIC_CLERK_SIGN_UP_URL = '/sign-up'; | ||
|
|
||
| const locals = createLocals({ runtime: { env: undefined as unknown as InternalEnv } }); | ||
| const env = getClientSafeEnv(locals); | ||
|
|
||
| expect(env.domain).toBe('test.domain.com'); | ||
| expect(env.signInUrl).toBe('/sign-in'); | ||
| expect(env.signUpUrl).toBe('/sign-up'); | ||
|
|
||
| delete process.env.PUBLIC_CLERK_DOMAIN; | ||
| delete process.env.PUBLIC_CLERK_SIGN_IN_URL; | ||
| delete process.env.PUBLIC_CLERK_SIGN_UP_URL; | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think of having process.env have a higher precedence than import.meta.env?
My gut is that if someone defines this in their running environment that is what they intend vs. what they had it set to when they built an app. It would also make the runtime variables have higher priority like when deployed in Cloudflare Workers.