fix(react-router): read VITE env-var for dev-warning suppression in SPA mode#8682
Conversation
…NSOLE_WARNING in SPA mode
🦋 Changeset detectedLatest commit: cd28ad1 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.
|
@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: |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository YAML (base), Organization UI (inherited) Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
📝 WalkthroughWalkthroughThis PR adds support for reading the environment variable VITE_CLERK_UNSAFE_DISABLE_DEVELOPMENT_MODE_CONSOLE_WARNING directly on the client in React Router SPA mode. It imports getPublicEnvVariables, updates ReactRouterClerkProvider to fall back to the env var when Clerk internal state is unavailable, updates tests to mock SPA mode and ensure env stub cleanup, and adds a test asserting the env-var-driven flag is forwarded to InternalClerkProvider. Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Possibly related PRs
🚥 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 |
There was a problem hiding this comment.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
packages/react-router/src/client/__tests__/ClerkProvider.test.tsx (1)
19-24:⚠️ Potential issue | 🟠 Major | ⚡ Quick winFix SPA-mode coverage + make env cleanup reliable in ClerkProvider test
isSpaModeis mocked to always returnfalse(packages/react-router/src/client/__tests__/ClerkProvider.test.tsxlines 19-24), so the test on lines 76-92 never exercises the SPA-only branch (assertPublishableKeyInSpaMode); theunsafe_disableDevelopmentModeConsoleWarningvalue is computed from env regardless of SPA mode.vi.unstubAllEnvs()runs only at the end of the test body, so it can be skipped if an assertion fails; move it toafterEach(or usetry/finally).Suggested fix
+const isSpaModeMock = vi.fn(() => false); + vi.mock('../../utils/assert', () => ({ assertPublishableKeyInSpaMode: vi.fn(), assertValidClerkState: vi.fn(), - isSpaMode: () => false, + isSpaMode: isSpaModeMock, warnForSsr: vi.fn(), })); describe('ClerkProvider __internal_clerkUIUrl via clerkState', () => { beforeEach(() => { vi.clearAllMocks(); + isSpaModeMock.mockReturnValue(false); }); afterEach(() => { vi.clearAllMocks(); + vi.unstubAllEnvs(); }); it('reads VITE_CLERK_UNSAFE_DISABLE_DEVELOPMENT_MODE_CONSOLE_WARNING in SPA mode where clerkState is absent', async () => { + isSpaModeMock.mockReturnValue(true); vi.stubEnv('VITE_CLERK_UNSAFE_DISABLE_DEVELOPMENT_MODE_CONSOLE_WARNING', 'true'); const { ClerkProvider } = await import('../ReactRouterClerkProvider'); render( <ClerkProvider publishableKey='pk_test_xxx'> <div>Test</div> </ClerkProvider>, ); expect(mockClerkProvider).toHaveBeenCalledWith( expect.objectContaining({ unsafe_disableDevelopmentModeConsoleWarning: true, }), ); - vi.unstubAllEnvs(); });🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/react-router/src/client/__tests__/ClerkProvider.test.tsx` around lines 19 - 24, The test mocks isSpaMode to always return false, preventing the SPA-only branch in assertPublishableKeyInSpaMode from being exercised and also computes unsafe_disableDevelopmentModeConsoleWarning regardless of SPA mode; update the mock so isSpaMode returns true for the SPA-specific test (or make it configurable per test) to exercise assertPublishableKeyInSpaMode, and ensure env cleanup uses vi.unstubAllEnvs() in an afterEach hook (or wrap the test body with try/finally) so environment variables are reliably restored; locate usages of isSpaMode, assertPublishableKeyInSpaMode, unsafe_disableDevelopmentModeConsoleWarning and move vi.unstubAllEnvs() into afterEach.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@packages/react-router/src/client/ReactRouterClerkProvider.tsx`:
- Around line 106-108: The boolean fallback for
unsafe_disableDevelopmentModeConsoleWarning currently uses || which treats an
explicit false as absent; change the expression in ReactRouterClerkProvider (the
unsafe_disableDevelopmentModeConsoleWarning assignment that references
__unsafeDisableDevelopmentModeConsoleWarning and
getPublicEnvVariables(...).unsafeDisableDevelopmentModeConsoleWarning) to use
the nullish coalescing operator (??) so that only null/undefined falls back to
the env value and an explicit false from the clerk state is preserved.
---
Outside diff comments:
In `@packages/react-router/src/client/__tests__/ClerkProvider.test.tsx`:
- Around line 19-24: The test mocks isSpaMode to always return false, preventing
the SPA-only branch in assertPublishableKeyInSpaMode from being exercised and
also computes unsafe_disableDevelopmentModeConsoleWarning regardless of SPA
mode; update the mock so isSpaMode returns true for the SPA-specific test (or
make it configurable per test) to exercise assertPublishableKeyInSpaMode, and
ensure env cleanup uses vi.unstubAllEnvs() in an afterEach hook (or wrap the
test body with try/finally) so environment variables are reliably restored;
locate usages of isSpaMode, assertPublishableKeyInSpaMode,
unsafe_disableDevelopmentModeConsoleWarning and move vi.unstubAllEnvs() into
afterEach.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository YAML (base), Organization UI (inherited)
Review profile: CHILL
Plan: Pro
Run ID: f357fa39-c614-44ff-aa32-99c577af5c47
📒 Files selected for processing (3)
.changeset/spa-dev-warning-env.mdpackages/react-router/src/client/ReactRouterClerkProvider.tsxpackages/react-router/src/client/__tests__/ClerkProvider.test.tsx
Follow-up to #8402, thanks to @jamesbvaughan for flagging this. The
VITE_CLERK_UNSAFE_DISABLE_DEVELOPMENT_MODE_CONSOLE_WARNINGshortcut only kicked in for SSR setups, because the env var is only read insidegetPublicEnvVariableson the server and threaded through therootAuthLoaderpayload. Client-only react-router has no loader, soclerkStateis undefined and the flag was always undefined on the provider.The fix is small: when
__unsafeDisableDevelopmentModeConsoleWarningis absent fromclerkState, fall back togetPublicEnvVariables(undefined).getEnvVariablealready handlesimport.meta.env, so the same lookup works on the client. Explicit<ClerkProvider unsafe_disableDevelopmentModeConsoleWarning>props still win since they are spread last intorestProps.