Skip to content

fix(vue): Defer useOrganization environment check until Clerk is loaded#7982

Merged
wobsoriano merged 3 commits intomainfrom
rob/vue-use-organization
Mar 4, 2026
Merged

fix(vue): Defer useOrganization environment check until Clerk is loaded#7982
wobsoriano merged 3 commits intomainfrom
rob/vue-use-organization

Conversation

@wobsoriano
Copy link
Member

@wobsoriano wobsoriano commented Mar 4, 2026

Description

  • Fixes a race condition where useOrganization checked environment settings before Clerk.load() resolved, causing the "Organizations feature required" dialog to appear (even when organizations is enabled, and a session is active)
  • Watches the loaded ref from the plugin context instead of the clerk ref

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

    • Fixed organization-related warnings and dialogs appearing incorrectly due to timing during Clerk initialization.
  • New Features

    • Exposes a reactive "loaded" state through the Vue plugin and injection, and adds it to the public injection type.
  • Tests

    • Added tests verifying behavior before and after the Clerk loaded state, ensuring environment checks run only after loading.

@changeset-bot
Copy link

changeset-bot bot commented Mar 4, 2026

🦋 Changeset detected

Latest commit: 0b48248

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

This PR includes changesets to release 2 packages
Name Type
@clerk/vue Patch
@clerk/nuxt 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

@vercel
Copy link

vercel bot commented Mar 4, 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 Mar 4, 2026 11:43pm

Request Review

@github-actions github-actions bot added the vue label Mar 4, 2026
if (value) {
// Optional chaining is important for `@clerk/vue` usage with older clerk-js versions that don't have the method
value.__internal_attemptToEnableEnvironmentSetting?.({
clerk.value?.__internal_attemptToEnableEnvironmentSetting?.({
Copy link
Member Author

Choose a reason for hiding this comment

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

We are confident that clerk is defined here

@wobsoriano wobsoriano requested a review from LauraBeatris March 4, 2026 23:03
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 4, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro

Run ID: 32ce5097-ae36-47e5-af2c-3364e9d5331d

📥 Commits

Reviewing files that changed from the base of the PR and between 5342885 and 0b48248.

📒 Files selected for processing (1)
  • packages/vue/src/composables/__tests__/useOrganization.test.ts

📝 Walkthrough

Walkthrough

The Vue Clerk package was updated to fix premature environment checks in the useOrganization composable by introducing and exposing a reactive loaded reference via the plugin injection and switching the composable to observe loaded for initialization logic. The patch replaces direct Clerk instance checks with loaded-based checks, adjusts internal calls accordingly, adds a unit test for the behavior, and includes a changeset entry.

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main change: deferring the useOrganization environment check until Clerk is loaded, which directly addresses the race condition bug fixed in the PR.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ 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.

*/
export const useOrganization: UseOrganization = () => {
const { clerk, organizationCtx } = useClerkContext('useOrganization');
const { loaded, clerk, organizationCtx } = useClerkContext('useOrganization');
Copy link
Member Author

Choose a reason for hiding this comment

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

This loaded ref is set to true when Clerk.load() succeeds so this is more reliable

@pkg-pr-new
Copy link

pkg-pr-new bot commented Mar 4, 2026

Open in StackBlitz

@clerk/agent-toolkit

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

@clerk/astro

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

@clerk/backend

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

@clerk/chrome-extension

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

@clerk/clerk-js

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

@clerk/dev-cli

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

@clerk/expo

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

@clerk/expo-passkeys

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

@clerk/express

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

@clerk/fastify

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

@clerk/hono

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

@clerk/localizations

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

@clerk/nextjs

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

@clerk/nuxt

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

@clerk/react

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

@clerk/react-router

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

@clerk/shared

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

@clerk/tanstack-react-start

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

@clerk/testing

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

@clerk/ui

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

@clerk/upgrade

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

@clerk/vue

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

commit: 0b48248

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
packages/vue/src/composables/useOrganization.ts (1)

55-105: ⚠️ Potential issue | 🟠 Major

Add regression coverage for this race-condition fix before merge.

This change alters timing-sensitive behavior in useOrganization, but no tests were added/updated to verify it. Please add a test that asserts the environment-setting check is deferred until Clerk is loaded (and does not run before load completes).

As per coding guidelines **/*: "If there are no tests added or modified as part of the PR, please suggest that tests be added to cover the changes."

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/vue/src/composables/useOrganization.ts` around lines 55 - 105, Add a
regression test for useOrganization that verifies the environment-setting call
clerk.value?.__internal_attemptToEnableEnvironmentSetting is not invoked until
loaded becomes true: mock a Clerk instance with a spy on
__internal_attemptToEnableEnvironmentSetting, mount or call useOrganization with
loaded initially false, assert the spy was not called, then set loaded to true
(or emit the watcher) and assert the spy is called once with the expected
payload ({ for: 'organizations', caller: 'useOrganization' }); place the test
alongside other vue composable tests and use the same mocking utilities used
elsewhere for clerk/session so it runs deterministically in CI.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@packages/vue/src/composables/useOrganization.ts`:
- Around line 55-105: Add a regression test for useOrganization that verifies
the environment-setting call
clerk.value?.__internal_attemptToEnableEnvironmentSetting is not invoked until
loaded becomes true: mock a Clerk instance with a spy on
__internal_attemptToEnableEnvironmentSetting, mount or call useOrganization with
loaded initially false, assert the spy was not called, then set loaded to true
(or emit the watcher) and assert the spy is called once with the expected
payload ({ for: 'organizations', caller: 'useOrganization' }); place the test
alongside other vue composable tests and use the same mocking utilities used
elsewhere for clerk/session so it runs deterministically in CI.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro

Run ID: ba872d41-0b8c-43ed-8867-1595f0889885

📥 Commits

Reviewing files that changed from the base of the PR and between b94a271 and 5342885.

📒 Files selected for processing (4)
  • .changeset/breezy-stingrays-give.md
  • packages/vue/src/composables/useOrganization.ts
  • packages/vue/src/plugin.ts
  • packages/vue/src/types.ts

@wobsoriano
Copy link
Member Author

!snapshot

@clerk-cookie
Copy link
Collaborator

Hey @wobsoriano - the snapshot version command generated the following package versions:

Package Version
@clerk/agent-toolkit 0.3.2-snapshot.v20260304234655
@clerk/astro 3.0.2-snapshot.v20260304234655
@clerk/backend 3.0.2-snapshot.v20260304234655
@clerk/chrome-extension 3.0.2-snapshot.v20260304234655
@clerk/clerk-js 6.0.1-snapshot.v20260304234655
@clerk/dev-cli 0.1.1-snapshot.v20260304234655
@clerk/expo 3.0.2-snapshot.v20260304234655
@clerk/expo-passkeys 1.0.2-snapshot.v20260304234655
@clerk/express 2.0.2-snapshot.v20260304234655
@clerk/fastify 3.0.2-snapshot.v20260304234655
@clerk/hono 0.0.4-snapshot.v20260304234655
@clerk/localizations 4.0.1-snapshot.v20260304234655
@clerk/msw 0.0.2-snapshot.v20260304234655
@clerk/nextjs 7.0.2-snapshot.v20260304234655
@clerk/nuxt 2.0.2-snapshot.v20260304234655
@clerk/react 6.0.2-snapshot.v20260304234655
@clerk/react-router 3.0.2-snapshot.v20260304234655
@clerk/shared 4.0.1-snapshot.v20260304234655
@clerk/tanstack-react-start 1.0.2-snapshot.v20260304234655
@clerk/testing 2.0.2-snapshot.v20260304234655
@clerk/ui 1.0.2-snapshot.v20260304234655
@clerk/upgrade 2.0.2-snapshot.v20260304234655
@clerk/vue 2.0.2-snapshot.v20260304234655

Tip: Use the snippet copy button below to quickly install the required packages.
@clerk/agent-toolkit

npm i @clerk/agent-toolkit@0.3.2-snapshot.v20260304234655 --save-exact

@clerk/astro

npm i @clerk/astro@3.0.2-snapshot.v20260304234655 --save-exact

@clerk/backend

npm i @clerk/backend@3.0.2-snapshot.v20260304234655 --save-exact

@clerk/chrome-extension

npm i @clerk/chrome-extension@3.0.2-snapshot.v20260304234655 --save-exact

@clerk/clerk-js

npm i @clerk/clerk-js@6.0.1-snapshot.v20260304234655 --save-exact

@clerk/dev-cli

npm i @clerk/dev-cli@0.1.1-snapshot.v20260304234655 --save-exact

@clerk/expo

npm i @clerk/expo@3.0.2-snapshot.v20260304234655 --save-exact

@clerk/expo-passkeys

npm i @clerk/expo-passkeys@1.0.2-snapshot.v20260304234655 --save-exact

@clerk/express

npm i @clerk/express@2.0.2-snapshot.v20260304234655 --save-exact

@clerk/fastify

npm i @clerk/fastify@3.0.2-snapshot.v20260304234655 --save-exact

@clerk/hono

npm i @clerk/hono@0.0.4-snapshot.v20260304234655 --save-exact

@clerk/localizations

npm i @clerk/localizations@4.0.1-snapshot.v20260304234655 --save-exact

@clerk/msw

npm i @clerk/msw@0.0.2-snapshot.v20260304234655 --save-exact

@clerk/nextjs

npm i @clerk/nextjs@7.0.2-snapshot.v20260304234655 --save-exact

@clerk/nuxt

npm i @clerk/nuxt@2.0.2-snapshot.v20260304234655 --save-exact

@clerk/react

npm i @clerk/react@6.0.2-snapshot.v20260304234655 --save-exact

@clerk/react-router

npm i @clerk/react-router@3.0.2-snapshot.v20260304234655 --save-exact

@clerk/shared

npm i @clerk/shared@4.0.1-snapshot.v20260304234655 --save-exact

@clerk/tanstack-react-start

npm i @clerk/tanstack-react-start@1.0.2-snapshot.v20260304234655 --save-exact

@clerk/testing

npm i @clerk/testing@2.0.2-snapshot.v20260304234655 --save-exact

@clerk/ui

npm i @clerk/ui@1.0.2-snapshot.v20260304234655 --save-exact

@clerk/upgrade

npm i @clerk/upgrade@2.0.2-snapshot.v20260304234655 --save-exact

@clerk/vue

npm i @clerk/vue@2.0.2-snapshot.v20260304234655 --save-exact

@wobsoriano wobsoriano enabled auto-merge (squash) March 4, 2026 23:50
@wobsoriano wobsoriano merged commit 98929f5 into main Mar 4, 2026
41 checks passed
@wobsoriano wobsoriano deleted the rob/vue-use-organization branch March 4, 2026 23:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants