Skip to content

fix(nextjs): settle onBeforeSetActive when cache invalidation fails - #10852

Open
wobsoriano wants to merge 6 commits into
mainfrom
rob/nextjs-before-set-active-rejection
Open

wobsoriano wants to merge 6 commits into
mainfrom
rob/nextjs-before-set-active-rejection

Conversation

@wobsoriano

Copy link
Copy Markdown
Member

Description

Takeover of #10088 by @RaphaelFakhri, who found and fixed this. Their commits are kept as-is.

Added in the takeover:

  • window.__internal_onBeforeSetActive no longer calls router.refresh() when invalidateCacheAction() fails. In a tab from a previous deployment, that refresh made Next.js reload the whole page while signOut() was removing the session. In a local repro after a redeploy, the user stayed signed in on 5 of 5 sign-out attempts. The hook now only resolves. window.__internal_onAfterSetActive still refreshes the router after the transition.

Fixes #9987

Checklist

  • pnpm test runs as expected.
  • pnpm build runs as expected.
  • (If applicable) JSDoc comments have been added or updated for any package exports
  • (If applicable) Documentation has been updated

Type of change

  • 🐛 Bug fix
  • 🌟 New feature
  • 🔨 Breaking change
  • 📖 Refactoring / dependency upgrade / documentation
  • other:

🤖 Generated with Claude under Rob's supervision.

RaphaelFakhri and others added 4 commits October 6, 2026 06:17
In a tab from a previous deployment, router.refresh() gets a payload
from the new build, and Next.js falls back to a full page reload. The
reload starts while signOut() removes the session or setActive()
navigates, and can stop the request before it completes. The
after-transition hook already refreshes the router.

Co-authored-by: Raphael Fakhri <153192858+RaphaelFakhri@users.noreply.github.com>
@vercel

vercel Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
swingset Ready Ready Preview Nov 17, 2026 4:02am UTC
1 Skipped Deployment
Project Deployment Actions Updated
clerk-js-sandbox Skipped Skipped Nov 17, 2026 4:02am UTC

Request Review

@changeset-bot

changeset-bot Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 425cb08

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@clerk/nextjs Patch
@clerk/swingset Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai

coderabbitai Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The App Router provider now waits for cache invalidation and settles its pre-activation hook when the action succeeds or rejects. For sign-out intents on Next.js 15 and 16, the hook skips cache invalidation. Tests cover these cases and verify that router refresh is not called. A patch changeset documents the fix for @clerk/nextjs.

Estimated code review effort: 2 (Simple) | ~8 minutes

Severity of issue fixed: Medium

Possibly related PRs

  • clerk/javascript#10088: Changes the same provider hook to settle when cache invalidation rejects and links the same issue.

Suggested reviewers: dstaley, brkalow

Merge Risk: Minimal · up to 425cb

This fix prevents sign-in, reverification and sign-out from hanging when the cache-invalidation server action fails, for example after a redeploy. The change is small, covered by tests, and easy to revert, so merge risk is minimal.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: the onBeforeSetActive hook in @clerk/nextjs now settles when cache invalidation fails. The changeset summary, ClerkProvider.tsx change, and tests all match this scope.…
Description check Passed The description explains the change in detail. It says the hook no longer calls router.refresh() when invalidateCacheAction() fails, and it gives the redeploy repro and the linked issue #9987. It also…
Linked Issues check Passed Issue #9987 requires window.__internal_onBeforeSetActive to settle when invalidateCacheAction() rejects, so that setActive, sign-in, reverification and signOut() do not hang. The whole-PR diff against…
Out of Scope Changes check Passed The whole-PR changes cover the hook change in ClerkProvider.tsx, unit tests in ClerkProvider.test.tsx for the same hook, and a patch changeset for @clerk/nextjs. The source diff changes only the __int…
Full details: Title check

Explanation

The title names the main change: the onBeforeSetActive hook in @clerk/nextjs now settles when cache invalidation fails. The changeset summary, ClerkProvider.tsx change, and tests all match this scope. The title uses the conventional fix(nextjs) prefix and is concise.

Full details: Description check

Explanation

The description explains the change in detail. It says the hook no longer calls router.refresh() when invalidateCacheAction() fails, and it gives the redeploy repro and the linked issue #9987. It also covers the takeover of #10088 and the test results, so it matches the changeset.

Full details: Linked Issues check

Explanation

Issue #9987 requires window.__internal_onBeforeSetActive to settle when invalidateCacheAction() rejects, so that setActive, sign-in, reverification and signOut() do not hang. The whole-PR diff against the merge base changes packages/nextjs/src/app-router/client/ClerkProvider.tsx. The hook is now async. On the Next 15/16 sign-out path it returns. Otherwise it runs await invalidateCacheAction().catch(() =&gt; {}), so it resolves on both fulfillment and rejection. This matches the issue's suggested fix. The hook does not call router.refresh(), and window.__internal_onAfterSetActive still refreshes the router, as the issue expects. The summary reports unit tests in ClerkProvider.test.tsx for the rejection case. I did not inspect the test file. The source change alone establishes the coding requirement. The PR description says the hook 'no longer calls router.refresh()'. The diff shows no such call at the merge base either, so that wording describes earlier commits. This does not affect the requirement.

Full details: Out of Scope Changes check

Explanation

The whole-PR changes cover the hook change in ClerkProvider.tsx, unit tests in ClerkProvider.test.tsx for the same hook, and a patch changeset for @clerk/nextjs. The source diff changes only the __internal_onBeforeSetActive hook body and its async signature. The added code comments keep the existing explanation, moved outside the Promise wrapper. The tests and changeset support the bug fix. No unrelated files or public API changes appear in the summary.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Linked repositories: Some configured repositories could not be included in this review. Cross-repository analysis may be incomplete. Please check linked-repository access and settings.

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 078cacc and 425cb08.

Files selected for processing: 2

  • packages/nextjs/src/app-router/client/ClerkProvider.tsx
  • packages/nextjs/src/app-router/client/__tests__/ClerkProvider.test.tsx
Linked repositories identified

CodeRabbit considers these linked repositories for cross-repo context during reviews:

Included review availability: This review used your included allowance. 7 included reviews remain after this review. Your included PR review attempts over the past 7 days set your current allowance at 8 reviews per hour.

@pkg-pr-new

pkg-pr-new Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@clerk/astro

npm i https://pkg.pr.new/@clerk/astro@10852

@clerk/backend

npm i https://pkg.pr.new/@clerk/backend@10852

@clerk/chrome-extension

npm i https://pkg.pr.new/@clerk/chrome-extension@10852

@clerk/clerk-js

npm i https://pkg.pr.new/@clerk/clerk-js@10852

@clerk/electron

npm i https://pkg.pr.new/@clerk/electron@10852

@clerk/electron-passkeys

npm i https://pkg.pr.new/@clerk/electron-passkeys@10852

@clerk/eslint-plugin

npm i https://pkg.pr.new/@clerk/eslint-plugin@10852

@clerk/expo

npm i https://pkg.pr.new/@clerk/expo@10852

@clerk/expo-biometrics

npm i https://pkg.pr.new/@clerk/expo-biometrics@10852

@clerk/expo-google-signin

npm i https://pkg.pr.new/@clerk/expo-google-signin@10852

@clerk/expo-native-components

npm i https://pkg.pr.new/@clerk/expo-native-components@10852

@clerk/expo-passkeys

npm i https://pkg.pr.new/@clerk/expo-passkeys@10852

@clerk/express

npm i https://pkg.pr.new/@clerk/express@10852

@clerk/fastify

npm i https://pkg.pr.new/@clerk/fastify@10852

@clerk/hono

npm i https://pkg.pr.new/@clerk/hono@10852

@clerk/localizations

npm i https://pkg.pr.new/@clerk/localizations@10852

@clerk/mosaic

npm i https://pkg.pr.new/@clerk/mosaic@10852

@clerk/nextjs

npm i https://pkg.pr.new/@clerk/nextjs@10852

@clerk/nuxt

npm i https://pkg.pr.new/@clerk/nuxt@10852

@clerk/react

npm i https://pkg.pr.new/@clerk/react@10852

@clerk/react-router

npm i https://pkg.pr.new/@clerk/react-router@10852

@clerk/shared

npm i https://pkg.pr.new/@clerk/shared@10852

@clerk/tanstack-react-start

npm i https://pkg.pr.new/@clerk/tanstack-react-start@10852

@clerk/testing

npm i https://pkg.pr.new/@clerk/testing@10852

@clerk/ui

npm i https://pkg.pr.new/@clerk/ui@10852

@clerk/upgrade

npm i https://pkg.pr.new/@clerk/upgrade@10852

@clerk/vue

npm i https://pkg.pr.new/@clerk/vue@10852

commit: 425cb08

@github-actions

github-actions Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

API Changes Report

Generated by Break Check on 2026-11-17T04:06:58.952Z

Summary

Metric Count
Packages analyzed 20
Packages with changes 0
🔴 Breaking changes 0
🟡 Non-breaking changes 0
🟢 Additions 0

No API Changes Detected

All packages have stable APIs with no detected changes.


Report generated by Break Check

Last ran on 425cb08.

@blacksmith-sh

blacksmith-sh Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Matches setActive no longer hangs when cache invalidation fails

With the server action's request failing, the hook stayed pending for over a minute before and resolved at once after, with no router refresh request following.

Merge-base

Server unreachable

This PR 425cb088

Server unreachable

Proof on 425cb088 (from 078cacce) · not run: Clerk Frontend API (needs NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY, CLERK_SECRET_KEY) · Did Proof get it right? Yes · No

View full Proof report · React 👎 or comment @codesmith dismiss this proof to dismiss

wobsoriano and others added 2 commits November 16, 2026 19:59
Co-authored-by: Raphael Fakhri <153192858+RaphaelFakhri@users.noreply.github.com>
Co-authored-by: Raphael Fakhri <153192858+RaphaelFakhri@users.noreply.github.com>

This branch was successfully deployed

1 active and 1 inactive deployments
Preview – swingset — 425cb088 Deployed Nov 17, 2026 by vercel[bot]
Preview – clerk-js-sandbox — 425cb088 Deployed Nov 17, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

@clerk/nextjs: onBeforeSetActive never settles when invalidateCacheAction rejects (e.g. after a redeploy), so setActive and signOut hang forever

2 participants