A bug was reported by @raf wherein if you opened two tabs of the Metal website and switched Clerk organizations in one of them, that switch didn't get synced to the second one. This is demonstrated in his screen recording (named rafs-recording.mp4).
I tried various things to fix this, but they were all unsuccessful. The surprising thing is that reloading the stale tab also doesn't make it get the correct active organization, as can be seen in my own screen recording (named folushos-recording.mp4)
Here's a list of the things I tried.
I set up useVisibilityChange from the @uidotdev package. With that, I was able to monitor when a user left and/or returned to a tab. Then, when visibility changed from false to true, I tried:
calling useOrganization().organization?.reload() to make the app reload orgs (see docs). No luck.
calling useOrganizationList().setActive?.({ session, organization}) (see docs).
On inspection of the polled calls to fetchServerOrgAndSession, I confirmed that the issue was that one tab had an orgId that differed from the other. So I created an async function that could be used to get the server's copy of orgId and sessionId when next the stale tab was viewed. Then those values were to fed to setActive. It:
didn't take effect immediately and
when I mistakenly left the stale tab open for long enough, its org actually overrode the fresh one.
I also inspected the cookies on the client and found some of Clerk's internal ones. I thought about saving the new org in a cookie on-switch-organization, then fetching and setActive()-ing that value when the stale tab was next viewed. However, Nextjs only allows setting cookies in server code, and setActive (AFAIK) is meant for the client.
All the code related to the above attempts can be found in this commit. (I did a create-commit-and-then-revert dance, so no those changes didn't get merged.)
A bug was reported by @raf wherein if you opened two tabs of the Metal website and switched Clerk organizations in one of them, that switch didn't get synced to the second one. This is demonstrated in his screen recording (named rafs-recording.mp4).
I tried various things to fix this, but they were all unsuccessful. The surprising thing is that reloading the stale tab also doesn't make it get the correct active organization, as can be seen in my own screen recording (named folushos-recording.mp4)
Here's a list of the things I tried.
All the code related to the above attempts can be found in this commit. (I did a create-commit-and-then-revert dance, so no those changes didn't get merged.)
rafs-recording.mp4
folushos-recording.mp4