Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/public-emus-stay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/nextjs': patch
---

Fix infinite handshake redirect loop when deploying Next.js apps with Clerk development instances to Netlify
9 changes: 8 additions & 1 deletion packages/nextjs/src/server/clerkMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
} from '@clerk/backend/internal';
import { clerkFrontendApiProxy, DEFAULT_PROXY_PATH, matchProxyPath } from '@clerk/backend/proxy';
import { parsePublishableKey } from '@clerk/shared/keys';
import { handleNetlifyCacheInDevInstance } from '@clerk/shared/netlifyCacheHandler';
import { notFound as nextjsNotFound } from 'next/navigation';
import type { NextMiddleware, NextRequest } from 'next/server';
import { NextResponse } from 'next/server';
Expand Down Expand Up @@ -222,7 +223,13 @@ export const clerkMiddleware = ((...args: unknown[]): NextMiddleware | NextMiddl

const locationHeader = requestState.headers.get(constants.Headers.Location);
if (locationHeader) {
const res = NextResponse.redirect(locationHeader);
handleNetlifyCacheInDevInstance({
locationHeader,
requestStateHeaders: requestState.headers,
publishableKey: requestState.publishableKey,
});

const res = NextResponse.redirect(requestState.headers.get(constants.Headers.Location) || locationHeader);
Copy link
Member

Choose a reason for hiding this comment

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

We can just use locationHeader here 👀

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think so? this code looks a bit different from the other frameworks

I read the other framework's implementation, and the response is built with Response(null, { status: 307, headers: requestState.headers })

In Next.js, we use NextResponse.redirect(url) and explicitly skip copying the Location header.

I mean, if we reused the locationHeader value captured before the mutation, the param added by handleNetlifyCacheInDevInstance is lost

Copy link
Member

Choose a reason for hiding this comment

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

Copy. Looks like I have to revisit the helper. Thanks!

Copy link
Member

Choose a reason for hiding this comment

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

thanks for correcting me here! I just checked and saw we're updating the Location header so your implementation is perfect!

requestState.headers.forEach((value, key) => {
if (key === constants.Headers.Location) {
return;
Expand Down
Loading