Skip to content

Commit d7d0704

Browse files
authored
fix(nextjs): Support Next.js 16 cache invalidation on sign-out (#7211)
1 parent 42f0d95 commit d7d0704

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

.changeset/major-rabbits-reply.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clerk/nextjs': patch
3+
---
4+
5+
Extend cache invalidation noop to Next.js 16 during sign-out

packages/nextjs/src/app-router/client/ClerkProvider.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,14 @@ const NextClientClerkProvider = (props: NextClerkProviderProps) => {
9696
router.refresh();
9797
});
9898
}
99-
// On Next.js v15 calling a server action that returns a 404 error when deployed on Vercel is prohibited, failing with 405 status code.
100-
// When a user transitions from "signed in" to "singed out", we clear the `__session` cookie, then we call `__unstable__onBeforeSetActive`.
99+
// On Next.js v15+ calling a server action that returns a 404 error when deployed on Vercel is prohibited, failing with 405 status code.
100+
// When a user transitions from "signed in" to "signed out", we clear the `__session` cookie, then we call `__unstable__onBeforeSetActive`.
101101
// If we were to call `invalidateCacheAction` while the user is already signed out (deleted cookie), any page protected by `auth.protect()`
102102
// will result to the server action returning a 404 error (this happens because server actions inherit the protection rules of the page they are called from).
103103
// SOLUTION:
104-
// To mitigate this, since the router cache on version 15 is much less aggressive, we can treat this as a noop and simply resolve the promise.
104+
// To mitigate this, since the router cache on version 15+ is much less aggressive, we can treat this as a noop and simply resolve the promise.
105105
// Once `setActive` performs the navigation, `__unstable__onAfterSetActive` will kick in and perform a router.refresh ensuring shared layouts will also update with the correct authentication context.
106-
else if (nextVersion.startsWith('15') && intent === 'sign-out') {
106+
else if ((nextVersion.startsWith('15') || nextVersion.startsWith('16')) && intent === 'sign-out') {
107107
resolve(); // noop
108108
} else {
109109
void invalidateCacheAction().then(() => resolve());

0 commit comments

Comments
 (0)