Skip to content

refactor(nextjs): factor runHandlerWithRequestState out of baseNextMiddleware#8368

Merged
jacekradko merged 4 commits intomainfrom
jacek/sdk-70-factor-middleware
Apr 22, 2026
Merged

refactor(nextjs): factor runHandlerWithRequestState out of baseNextMiddleware#8368
jacekradko merged 4 commits intomainfrom
jacek/sdk-70-factor-middleware

Conversation

@jacekradko
Copy link
Copy Markdown
Member

@jacekradko jacekradko commented Apr 21, 2026

Summary

Starting work on fixing the issue with keyless mode skipping middleware auth checks.

Groundwork for SDK-70. Pure refactor — no behavioral change.

  • @clerk/backend — exports a new createBootstrapSignedOutState helper from @clerk/backend/internal. Returns a synthetic UnauthenticatedState<'session_token'> without requiring a publishable key or an AuthenticateContext. Intended for framework integrations that need to run authorization logic before real Clerk keys are available (e.g. the Next.js keyless bootstrap window).
  • @clerk/nextjs — factors the post-authentication pipeline inside baseNextMiddleware (handler invocation, CSP, redirects, response decoration) into a private runHandlerWithRequestState helper. The authenticateRequest call and subsequent pipeline now live in a single, testable unit, making it possible for a follow-up to feed a synthesized RequestState into the same pipeline when there's no real publishable key yet.

Why split this PR?

The follow-up (SDK-70 proper) will flip keylessMiddleware's no-key branch to synthesize a signed-out state and run the user's middleware handler, closing a middleware-bypass window during the keyless bootstrap. That change is small on its own — most of the work is the plumbing to route a non-authenticateRequest-produced RequestState through the same post-auth pipeline. Landing the plumbing first keeps the behavioral change's diff small and reviewable.

Test plan

  • pnpm turbo build --filter=@clerk/backend --filter=@clerk/nextjs passes
  • pnpm --filter=@clerk/backend test — 1186/1186 pass (includes the updated exports.test.ts snapshot)
  • pnpm --filter=@clerk/nextjs test — 369 pass / 50 fail; all 50 failures are a pre-existing AbortSignal/createClerkRequest test-env issue that reproduces identically on main (unrelated to this refactor)
  • Manual smoke: keyless bootstrap still works end-to-end (no behavioral change expected since both call sites in baseNextMiddleware were preserved)

…ddleware

Extracts the post-authentication pipeline (handler invocation, CSP, redirects,
response decoration) into a private helper. Also adds createBootstrapSignedOutState
to @clerk/backend/internal for synthesizing a signed-out RequestState without a
publishable key — intended for framework integrations that must run authorization
logic before real Clerk keys are available (e.g. the Next.js keyless bootstrap
window).

Pure refactor — no behavioral change.
@vercel
Copy link
Copy Markdown

vercel Bot commented Apr 21, 2026

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

Project Deployment Actions Updated (UTC)
clerk-js-sandbox Ready Ready Preview, Comment Apr 22, 2026 3:56pm

Request Review

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Apr 21, 2026

🦋 Changeset detected

Latest commit: 2d6d771

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

This PR includes changesets to release 10 packages
Name Type
@clerk/backend Minor
@clerk/nextjs Patch
@clerk/astro Patch
@clerk/express Patch
@clerk/fastify Patch
@clerk/hono Patch
@clerk/nuxt Patch
@clerk/react-router Patch
@clerk/tanstack-react-start Patch
@clerk/testing 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
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 21, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro

Run ID: 5524ca7e-1384-4ae0-b16e-c01675f646b2

📥 Commits

Reviewing files that changed from the base of the PR and between 6060ef2 and 2d6d771.

📒 Files selected for processing (2)
  • .changeset/sdk-70-backend-bootstrap-state.md
  • packages/backend/src/tokens/authStatus.ts
✅ Files skipped from review due to trivial changes (1)
  • .changeset/sdk-70-backend-bootstrap-state.md

📝 Walkthrough

Walkthrough

Adds a new internal helper createBootstrapSignedOutState in @clerk/backend that returns a synthetic UnauthenticatedState<'session_token'> for keyless bootstrap scenarios; the symbol is re-exported and its presence is verified by tests. Separately, refactors @clerk/nextjs middleware by extracting the post-authentication pipeline into a private runHandlerWithRequestState helper, preserving existing behavior and not changing public APIs.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main refactor: extracting the post-authentication pipeline into runHandlerWithRequestState helper in the Next.js middleware.
Description check ✅ Passed The description clearly explains the changes: new createBootstrapSignedOutState export in @clerk/backend and the refactoring of clerkMiddleware internals, including rationale and test results.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Comment @coderabbitai help to get the list of available commands and usage tips.

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Apr 21, 2026

Open in StackBlitz

@clerk/astro

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

@clerk/backend

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

@clerk/chrome-extension

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

@clerk/clerk-js

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

@clerk/dev-cli

npm i https://pkg.pr.new/@clerk/dev-cli@8368

@clerk/expo

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

@clerk/expo-passkeys

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

@clerk/express

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

@clerk/fastify

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

@clerk/hono

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

@clerk/localizations

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

@clerk/nextjs

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

@clerk/nuxt

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

@clerk/react

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

@clerk/react-router

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

@clerk/shared

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

@clerk/tanstack-react-start

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

@clerk/testing

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

@clerk/ui

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

@clerk/upgrade

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

@clerk/vue

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

commit: 2d6d771

Copy link
Copy Markdown
Member

@wobsoriano wobsoriano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks straightforward and good to me 👍🏼 thanks

The helper hard-coded isSatellite/domain/proxyUrl, which meant a satellite
keyless app would lose the __clerk_status=needs-sync marker that createRedirect
adds to cross-origin sign-in return URLs. Accept them as optional params so
callers can forward their resolved middleware options.
@jacekradko jacekradko merged commit 93855c2 into main Apr 22, 2026
42 checks passed
@jacekradko jacekradko deleted the jacek/sdk-70-factor-middleware branch April 22, 2026 16:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants