Skip to content

Instantly share code, notes, and snippets.

@ezhong0
Created October 28, 2026 07:11
Show Gist options
  • Select an option

  • Save ezhong0/340ae2483c3ccb40cc5865171c2b4858 to your computer and use it in GitHub Desktop.

Select an option

Save ezhong0/340ae2483c3ccb40cc5865171c2b4858 to your computer and use it in GitHub Desktop.
Minimal reproduction: Clerk script loader resolves but retry emits an unhandled rejection

Clerk script loader detached rejection reproduction

Minimal synthetic reproduction against the real published @clerk/shared package. It uses fake DOM objects and timers; it makes no script requests, uses no real Clerk application or credentials, and needs no browser.

With Node 24.22.0 and pnpm 10.33.0:

pnpm install --ignore-scripts
pnpm repro js late-success
pnpm repro ui late-success
pnpm repro js failure

The script intentionally exits 1 when it observes an unhandled rejection. It attaches both success and failure handlers to the promise returned by the loader.

For late-success, another loader is simulated making Clerk available after 100ms. The pending script attempts fail after 200ms each. The returned load operation resolves, but the script retry chain still emits an unhandled failed_to_load_clerk_js (or failed_to_load_clerk_ui). Expected: the returned operation remains resolved, with no unhandled rejection.

For failure, no Clerk global appears. Expected: the returned operation rejects, without an additional unhandled rejection.

The key is synthetically generated for probe.clerk.accounts.dev; it does not identify a real application. The unhandledRejection listener only observes the bug for this reproduction; it is not a proposed application workaround.

{
"name": "clerk-loader-rejection-reproduction",
"private": true,
"packageManager": "[email protected]",
"scripts": {
"repro": "node repro.cjs"
},
"dependencies": {
"@clerk/shared": "4.43.0",
"@sinonjs/fake-timers": "15.4.0"
}
}
const FakeTimers = require('@sinonjs/fake-timers');
async function main() {
const kind = process.argv[2] || 'js';
const scenario = process.argv[3] || 'late-success';
const unhandled = [];
process.on('unhandledRejection', error => unhandled.push(error.code || error.message));
const clock = FakeTimers.install({ toFake: ['setTimeout', 'clearTimeout', 'setInterval', 'clearInterval'] });
let attempts = 0;
global.window = {};
global.document = {
querySelector: () => null,
createElement: () => {
const handlers = {};
return { handlers, setAttribute() {}, remove() {}, addEventListener(name, handler) { handlers[name] = handler; } };
},
body: { appendChild(script) {
attempts++;
setTimeout(() => script.handlers.error({ error: new Error('synthetic script failure') }), 200);
} },
};
const sdk = await import('@clerk/shared/loadClerkJsScript');
const load = kind === 'ui' ? sdk.loadClerkUIScript : sdk.loadClerkJsScript;
let result = 'pending';
const publishableKey = 'pk_test_' + Buffer.from('probe.clerk.accounts.dev$').toString('base64');
load({ publishableKey }).then(
() => { result = 'resolved'; },
error => { result = error.code || error.message; },
);
if (scenario === 'late-success') {
setTimeout(() => {
if (kind === 'ui') window.__internal_ClerkUICtor = {};
else window.Clerk = { load() {} };
}, 100);
}
await clock.tickAsync(60000);
clock.uninstall();
console.log(JSON.stringify({ kind, scenario, result, attempts, unhandled }, null, 2));
if (unhandled.length) process.exitCode = 1;
}
main().catch(error => { console.error(error); process.exitCode = 1; });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment