Skip to content

Commit 5f9159c

Browse files
committed
Revalidate server state
1 parent 94649ad commit 5f9159c

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1290,6 +1290,15 @@ export class Clerk implements ClerkInterface {
12901290
eventBus.emit(events.TokenUpdate, { token: null });
12911291
}
12921292

1293+
/**
1294+
* Invalidate previously cache pages with auth state before navigating
1295+
*/
1296+
const onBeforeSetActive: SetActiveHook =
1297+
typeof window !== 'undefined' && typeof window.__unstable__onBeforeSetActive === 'function'
1298+
? window.__unstable__onBeforeSetActive
1299+
: noop;
1300+
await onBeforeSetActive();
1301+
12931302
// Only triggers navigation for internal AIO components routing or multi-session switch, in order to not affect custom flows
12941303
const isSwitchingSessions = this.session?.id != session.id;
12951304
const shouldNavigateOnSetActive = this.#componentNavigationContext || isSwitchingSessions;
@@ -1301,9 +1310,19 @@ export class Clerk implements ClerkInterface {
13011310
componentNavigationContext: this.#componentNavigationContext,
13021311
});
13031312
}
1304-
13051313
this.#setAccessors(session);
13061314
this.#emit();
1315+
1316+
/**
1317+
* Invoke the Next.js middleware to synchronize server and client state after resolving a session task.
1318+
* This ensures that any server-side logic depending on the session status (like middleware-based
1319+
* redirects or protected routes) correctly reflects the updated client authentication state.
1320+
*/
1321+
const onAfterSetActive: SetActiveHook =
1322+
typeof window !== 'undefined' && typeof window.__unstable__onAfterSetActive === 'function'
1323+
? window.__unstable__onAfterSetActive
1324+
: noop;
1325+
await onAfterSetActive();
13071326
};
13081327

13091328
public __experimental_navigateToTask = async ({ redirectUrlComplete }: NextTaskParams = {}): Promise<void> => {

0 commit comments

Comments
 (0)