Skip to content

Commit 061c6c4

Browse files
fix(nextjs): prevent infinite handshake redirect loop on Netlify (#7857)
Co-authored-by: Robert Soriano <sorianorobertc@gmail.com>
1 parent bebb14c commit 061c6c4

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

.changeset/public-emus-stay.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+
Fix infinite handshake redirect loop when deploying Next.js apps with Clerk development instances to Netlify

packages/nextjs/src/server/clerkMiddleware.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
} from '@clerk/backend/internal';
2323
import { clerkFrontendApiProxy, DEFAULT_PROXY_PATH, matchProxyPath } from '@clerk/backend/proxy';
2424
import { parsePublishableKey } from '@clerk/shared/keys';
25+
import { handleNetlifyCacheInDevInstance } from '@clerk/shared/netlifyCacheHandler';
2526
import { notFound as nextjsNotFound } from 'next/navigation';
2627
import type { NextMiddleware, NextRequest } from 'next/server';
2728
import { NextResponse } from 'next/server';
@@ -222,7 +223,13 @@ export const clerkMiddleware = ((...args: unknown[]): NextMiddleware | NextMiddl
222223

223224
const locationHeader = requestState.headers.get(constants.Headers.Location);
224225
if (locationHeader) {
225-
const res = NextResponse.redirect(locationHeader);
226+
handleNetlifyCacheInDevInstance({
227+
locationHeader,
228+
requestStateHeaders: requestState.headers,
229+
publishableKey: requestState.publishableKey,
230+
});
231+
232+
const res = NextResponse.redirect(requestState.headers.get(constants.Headers.Location) || locationHeader);
226233
requestState.headers.forEach((value, key) => {
227234
if (key === constants.Headers.Location) {
228235
return;

0 commit comments

Comments
 (0)