-
Notifications
You must be signed in to change notification settings - Fork 430
feat(react): Add env variable fallback for publishableKey in Vite apps #7634
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
Conversation
When publishableKey is not explicitly provided, the React SDK now checks for VITE_CLERK_PUBLISHABLE_KEY and CLERK_PUBLISHABLE_KEY environment variables. This enables Vite users to set their publishable key via environment variables without manual configuration. The fallback only triggers when publishableKey is undefined (not when it's an empty string) to avoid conflicting with framework SDKs like Next.js that may pass empty string when their env var is not set. Closes AIE-431 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
🦋 Changeset detectedLatest commit: 00dc058 The changes in this PR will be included in the next version bump. This PR includes changesets to release 6 packages
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.
|
📝 WalkthroughWalkthroughThe PR implements automatic environment variable fallback for the publishableKey in the Clerk React SDK. A new utility module is introduced that handles merging of options with environment variables, checking VITE_CLERK_PUBLISHABLE_KEY first, then CLERK_PUBLISHABLE_KEY. The ClerkContextProvider is updated to use this merging function. Comprehensive tests validate the fallback behavior and precedence rules, including scenarios where explicit options override environment variables and where empty strings are preserved. Integration template examples are simplified by removing explicit publishableKey props, relying instead on automatic detection. 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Comment |
@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/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: |
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
jacekradko
left a comment
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.
👍🏼
Extend the env var fallback support to include: - signInUrl (VITE_CLERK_SIGN_IN_URL / CLERK_SIGN_IN_URL) - signUpUrl (VITE_CLERK_SIGN_UP_URL / CLERK_SIGN_UP_URL) - signInForceRedirectUrl - signUpForceRedirectUrl - signInFallbackRedirectUrl - signUpFallbackRedirectUrl This brings the React SDK closer to parity with the Next.js SDK for Vite users who want to configure Clerk via environment variables. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
…elper Replace multiple individual env var getter functions with a single mergeWithEnv helper that handles all env var fallbacks in one place. This simplifies the isomorphicClerk code and mirrors the pattern used in the Next.js SDK with mergeNextClerkPropsWithEnv. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
…ntextProvider Move the mergeWithEnv call from IsomorphicClerk.getOrCreateInstance to ClerkContextProvider, where the options enter the public API. This: - Follows the same pattern as Next.js SDK (mergeNextClerkPropsWithEnv) - Keeps IsomorphicClerk focused on initialization, not configuration resolution - Makes the env var handling visible at the API boundary - Maintains clearer separation of concerns Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Move mergeWithEnv tests to dedicated file at src/utils/__tests__/envVariables.test.ts, keeping isomorphicClerk.test.ts focused on IsomorphicClerk-specific behavior. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
586ae45 to
9efdf64
Compare
…lback Remove explicit publishableKey props from ClerkProvider in react-vite and custom-flows-react-vite templates since ClerkProvider now automatically reads from VITE_CLERK_PUBLISHABLE_KEY env var. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
9efdf64 to
fa60cc1
Compare
When environment variables are not set, return undefined instead of empty string. This prevents overriding downstream defaults (like those in @clerk/react-router) with empty strings, which can cause different behavior than undefined. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
…ibrary mode URL environment variable fallbacks (signInUrl, signUpUrl, etc.) were causing compatibility issues with @clerk/react-router in library mode. Only publishableKey env var fallback is now supported. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Summary
VITE_CLERK_PUBLISHABLE_KEYandCLERK_PUBLISHABLE_KEYenvironment variables whenpublishableKeyis not explicitly provided toClerkProviderpublishableKeyisundefined(not empty string) to maintain compatibility with framework SDKs like Next.jsCloses AIE-431
Test plan
getPublishableKeyFromEnvutility functiongetOrCreateInstanceenv var fallback behavior🤖 Generated with Claude Code
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.