Skip to content

Commit

Permalink
fix(nextjs): Always use nextUrl to build rewritten URLs (#4856)
Browse files Browse the repository at this point in the history
  • Loading branch information
dstaley authored Jan 9, 2025
1 parent 0994963 commit ae17771
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/brave-steaks-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/nextjs': patch
---

Always use nextUrl to build rewritten URLs
14 changes: 11 additions & 3 deletions packages/nextjs/src/server/clerkMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export const clerkMiddleware: ClerkMiddleware = (...args: unknown[]) => {
);
handlerResult = userHandlerResult || handlerResult;
} catch (e: any) {
handlerResult = handleControlFlowErrors(e, clerkRequest, requestState);
handlerResult = handleControlFlowErrors(e, clerkRequest, request, requestState);
}

// TODO @nikos: we need to make this more generic
Expand Down Expand Up @@ -314,11 +314,19 @@ const createMiddlewareProtect = (
// especially when copy-pasting code from one place to another.
// This function handles the known errors thrown by the APIs described above,
// and returns the appropriate response.
const handleControlFlowErrors = (e: any, clerkRequest: ClerkRequest, requestState: RequestState): Response => {
const handleControlFlowErrors = (
e: any,
clerkRequest: ClerkRequest,
nextRequest: NextRequest,
requestState: RequestState,
): Response => {
if (isNextjsNotFoundError(e)) {
// Rewrite to a bogus URL to force not found error
return setHeader(
NextResponse.rewrite(`${clerkRequest.clerkUrl.origin}/clerk_${Date.now()}`),
// This is an internal rewrite purely to trigger a not found error. We do not want Next.js to think that the
// destination URL is a valid page, so we use `nextRequest.url` as the base for the fake URL, which Next.js
// understands is an internal URL and won't run middleware against the request.
NextResponse.rewrite(new URL(`/clerk_${Date.now()}`, nextRequest.url)),
constants.Headers.AuthReason,
'protect-rewrite',
);
Expand Down

0 comments on commit ae17771

Please sign in to comment.