fix(react): Detect nested ClerkProvider via context instead of a global mount counter - #9335
fix(react): Detect nested ClerkProvider via context instead of a global mount counter#9335wobsoriano wants to merge 4 commits into
Conversation
🦋 Changeset detectedLatest commit: 8efe61f The changes in this PR will be included in the next version bump. This PR includes changesets to release 7 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.
|
@clerk/astro
@clerk/backend
@clerk/chrome-extension
@clerk/clerk-js
@clerk/electron
@clerk/electron-passkeys
@clerk/eslint-plugin
@clerk/expo
@clerk/expo-google-signin
@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: |
API Changes Report
Summary
No API Changes DetectedAll packages have stable APIs with no detected changes. Report generated by Break Check Last ran on |
|
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 Plus Run ID: 📒 Files selected for processing (1)
🔗 Linked repositories identifiedCodeRabbit considers these linked repositories for cross-repo context during reviews:
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthrough
Estimated code review effort: 2 (Simple) | ~10 minutes Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
packages/react/src/contexts/__tests__/ClerkProvider.test.tsx (1)
27-27: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winReplace
anyin the mock singleton.Use
IsomorphicClerk | undefinedforinstanceto retain type checking for the shared mock.🤖 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/src/contexts/__tests__/ClerkProvider.test.tsx` at line 27, Update the mock singleton’s instance declaration in ClerkProvider tests to use IsomorphicClerk | undefined instead of any, preserving type checking for the shared mock while retaining its initially undefined state.Source: Coding guidelines
🤖 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.
Nitpick comments:
In `@packages/react/src/contexts/__tests__/ClerkProvider.test.tsx`:
- Line 27: Update the mock singleton’s instance declaration in ClerkProvider
tests to use IsomorphicClerk | undefined instead of any, preserving type
checking for the shared mock while retaining its initially undefined state.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository YAML (base), Organization UI (inherited)
Review profile: CHILL
Plan: Pro Plus
Run ID: 8306c9aa-8672-4edf-8711-dc58b7891e57
📒 Files selected for processing (1)
packages/react/src/contexts/__tests__/ClerkProvider.test.tsx
🔗 Linked repositories identified
CodeRabbit considers these linked repositories for cross-repo context during reviews:
clerk/clerk_go(manual)clerk/dashboard(manual)clerk/accounts(manual)clerk/backoffice(manual)clerk/clerk(manual)clerk/clerk-docs(manual)clerk/cloudflare-workers(manual)clerk/clerk-ios(auto-detected)clerk/clerk-android(auto-detected)clerk/cli(auto-detected)
Description
On Android, activity recreation (returning from a third-party SDK, a font size or locale change, the OS restoring a backgrounded app) briefly runs two React surfaces in one JS runtime.
ClerkProvider's duplicate guard counts mounts in a module-scoped map, so the new surface sees the old surface's mount and throwsmultipleClerkProvidersErrorin apps that render exactly one provider, crashing the whole app with no opt-out.ClerkProvidernow readsClerkInstanceContextduring render and throws only when genuinely nested inside another provider. Context is per tree, so a second root or surface can't false-positive. This mirrors theisNestedcheck@clerk/nextjshas used for years. The global counter predates theIsomorphicClerksingleton, when each provider injected its own clerk-js script and a duplicate anywhere genuinely broke the page.Reproduced and verified on an Android emulator with an Expo dev build. Rapid repeated config changes (font scale and dark mode toggles 0.4s apart) put two surfaces in flight and crashed the current release within two rounds. The same loop against a snapshot of this branch produced 24 surface restarts with zero errors and a healthy app.
Fixes https://clerkinc.slack.com/archives/C0B3EGPLNKH/p1785876391910099
Checklist
pnpm testruns as expected.pnpm buildruns as expected.Type of change