Skip to content
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

refactor(clerk-js): Make use of browser navigator locks property #5156

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/giant-ducks-bow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/clerk-js': patch
---

Make use of native browser navigator locks property to avoid needing to use localstorage or cookies.
14 changes: 14 additions & 0 deletions packages/clerk-js/src/core/auth/safeLock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@ export function SafeLock(key: string) {
});

const acquireLockAndRun = async (cb: () => unknown) => {
if ('locks' in navigator && isSecureContext) {
const controller = new AbortController();
const lockTimeout = setTimeout(() => controller.abort(), 4999);
Copy link
Member

Choose a reason for hiding this comment

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

Curious about the 4999 here. 🤔

Copy link
Member

Choose a reason for hiding this comment

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

It was one ms less than the the 5s polling interval 😅

return await navigator.locks
.request(key, { signal: controller.signal }, async () => {
clearTimeout(lockTimeout);
return await cb();
})
.catch(() => {
// browser-tabs-lock never seems to throw, so we are mirroring the behavior here
return false;
});
}

if (await lock.acquireLock(key, 5000)) {
try {
return await cb();
Expand Down