diff --git a/.changeset/modern-clocks-sip.md b/.changeset/modern-clocks-sip.md new file mode 100644 index 00000000000..0f285497349 --- /dev/null +++ b/.changeset/modern-clocks-sip.md @@ -0,0 +1,5 @@ +--- +'@clerk/clerk-js': patch +--- + +Fix server-side session cache not being invalidated for after-auth custom flows diff --git a/packages/clerk-js/bundlewatch.config.json b/packages/clerk-js/bundlewatch.config.json index cc0a673db41..3aa7288ea45 100644 --- a/packages/clerk-js/bundlewatch.config.json +++ b/packages/clerk-js/bundlewatch.config.json @@ -1,7 +1,7 @@ { "files": [ { "path": "./dist/clerk.js", "maxSize": "618KB" }, - { "path": "./dist/clerk.browser.js", "maxSize": "72.2KB" }, + { "path": "./dist/clerk.browser.js", "maxSize": "74KB" }, { "path": "./dist/clerk.legacy.browser.js", "maxSize": "115.08KB" }, { "path": "./dist/clerk.headless*.js", "maxSize": "55KB" }, { "path": "./dist/ui-common*.js", "maxSize": "113KB" }, diff --git a/packages/clerk-js/src/core/clerk.ts b/packages/clerk-js/src/core/clerk.ts index 3e9fc043dea..7bcc3910e05 100644 --- a/packages/clerk-js/src/core/clerk.ts +++ b/packages/clerk-js/src/core/clerk.ts @@ -1325,6 +1325,29 @@ export class Clerk implements ClerkInterface { }; #handlePendingSession = async (session: PendingSessionResource) => { + /** + * Do not revalidate server cache when `setActive` is called with a pending + * session within components, to avoid flash of content and unmount during + * internal navigation + */ + const shouldInvalidateCache = !this.#componentNavigationContext; + + const onBeforeSetActive: SetActiveHook = + shouldInvalidateCache && + typeof window !== 'undefined' && + typeof window.__unstable__onBeforeSetActive === 'function' + ? window.__unstable__onBeforeSetActive + : noop; + + const onAfterSetActive: SetActiveHook = + shouldInvalidateCache && + typeof window !== 'undefined' && + typeof window.__unstable__onAfterSetActive === 'function' + ? window.__unstable__onAfterSetActive + : noop; + + await onBeforeSetActive(); + if (!this.environment) { return; } @@ -1358,6 +1381,8 @@ export class Clerk implements ClerkInterface { this.#setAccessors(session); this.#emit(); + + await onAfterSetActive(); }; public __internal_navigateToTaskIfAvailable = async ({