Skip to content
Closed
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/remove-beforeunload-safelock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/clerk-js': patch
---

Removed redundant `beforeunload` event listener from SafeLock that was disabling the browser's back-forward cache (bfcache), degrading navigation performance.
26 changes: 15 additions & 11 deletions integration/tests/next-quickstart-keyless.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,24 @@ test.describe('Keyless mode @quickstart', () => {
await newPage.waitForLoadState();

await newPage.waitForURL(url => {
const urlToReturnTo = `${dashboardUrl}apps/claim?token=`;

const signInForceRedirectUrl = url.searchParams.get('sign_in_force_redirect_url');
const signUpForceRedirectUrl = url.searchParams.get('sign_up_force_redirect_url');

const signUpForceRedirectUrlCheck =
signUpForceRedirectUrl?.startsWith(urlToReturnTo) ||
(signUpForceRedirectUrl?.startsWith(`${dashboardUrl}prepare-account`) &&
signUpForceRedirectUrl?.includes(encodeURIComponent('apps/claim?token=')));
// Backend adds framework=nextjs query param before token, so use .includes() instead of .startsWith()
const signInHasRequiredParams =
signInForceRedirectUrl?.includes(`${dashboardUrl}apps/claim`) && signInForceRedirectUrl?.includes('token=');

const signUpRegularCase =
signUpForceRedirectUrl?.includes(`${dashboardUrl}apps/claim`) && signUpForceRedirectUrl?.includes('token=');

const signUpPrepareAccountCase =
signUpForceRedirectUrl?.startsWith(`${dashboardUrl}prepare-account`) &&
signUpForceRedirectUrl?.includes(encodeURIComponent('apps/claim')) &&
signUpForceRedirectUrl?.includes(encodeURIComponent('token='));

const signUpHasRequiredParams = signUpRegularCase || signUpPrepareAccountCase;

return (
url.pathname === '/apps/claim/sign-in' &&
url.searchParams.get('sign_in_force_redirect_url')?.startsWith(urlToReturnTo) &&
signUpForceRedirectUrlCheck
);
return url.pathname === '/apps/claim/sign-in' && signInHasRequiredParams && signUpHasRequiredParams;
});
});

Expand Down
6 changes: 0 additions & 6 deletions packages/clerk-js/src/core/auth/safeLock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@ import Lock from 'browser-tabs-lock';
export function SafeLock(key: string) {
const lock = new Lock();

// TODO: Figure out how to fix this linting error
// eslint-disable-next-line @typescript-eslint/no-misused-promises
window.addEventListener('beforeunload', async () => {
await lock.releaseLock(key);
});

const acquireLockAndRun = async (cb: () => Promise<unknown>) => {
if ('locks' in navigator && isSecureContext) {
const controller = new AbortController();
Expand Down
Loading