Skip to content

Commit d0d4128

Browse files
committed
fix: Revalidate auth state for Next.js server
1 parent acf4b69 commit d0d4128

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

.changeset/chatty-llamas-wink.md

Lines changed: 0 additions & 4 deletions
This file was deleted.

packages/clerk-js/bundlewatch.config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
{ "path": "./dist/clerk.js", "maxSize": "612kB" },
44
{ "path": "./dist/clerk.browser.js", "maxSize": "72.2KB" },
55
{ "path": "./dist/clerk.legacy.browser.js", "maxSize": "115KB" },
6-
{ "path": "./dist/clerk.headless*.js", "maxSize": "53.06KB" },
6+
{ "path": "./dist/clerk.headless*.js", "maxSize": "55KB" },
77
{ "path": "./dist/ui-common*.js", "maxSize": "108.56KB" },
88
{ "path": "./dist/vendors*.js", "maxSize": "40.2KB" },
99
{ "path": "./dist/coinbase*.js", "maxSize": "38KB" },

packages/clerk-js/src/core/clerk.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1283,7 +1283,16 @@ export class Clerk implements ClerkInterface {
12831283
};
12841284

12851285
public __experimental_navigateToTask = async ({ redirectUrlComplete }: NextTaskParams = {}): Promise<void> => {
1286-
const session = await this.session?.reload();
1286+
/**
1287+
* Invalidate previously cache pages with auth state before navigating
1288+
*/
1289+
const onBeforeSetActive: SetActiveHook =
1290+
typeof window !== 'undefined' && typeof window.__unstable__onBeforeSetActive === 'function'
1291+
? window.__unstable__onBeforeSetActive
1292+
: noop;
1293+
await onBeforeSetActive();
1294+
1295+
const session = this.session;
12871296
if (!session || !this.environment) {
12881297
return;
12891298
}
@@ -1311,6 +1320,17 @@ export class Clerk implements ClerkInterface {
13111320

13121321
this.#setAccessors(session);
13131322
this.#emit();
1323+
1324+
/**
1325+
* Invoke the Next.js middleware to synchronize server and client state after resolving a session task.
1326+
* This ensures that any server-side logic depending on the session status (like middleware-based
1327+
* redirects or protected routes) correctly reflects the updated client authentication state.
1328+
*/
1329+
const onAfterSetActive: SetActiveHook =
1330+
typeof window !== 'undefined' && typeof window.__unstable__onAfterSetActive === 'function'
1331+
? window.__unstable__onAfterSetActive
1332+
: noop;
1333+
await onAfterSetActive();
13141334
};
13151335

13161336
public addListener = (listener: ListenerCallback): UnsubscribeCallback => {

0 commit comments

Comments
 (0)