Skip to content

Commit b497388

Browse files
committed
check status code
1 parent ee6615c commit b497388

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

packages/open-next/src/core/routing/middleware.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ const middleMatch = getMiddlewareMatch(
2626
functionsConfigManifest,
2727
);
2828

29+
const REDIRECTS = new Set([301, 302, 303, 307, 308]);
30+
2931
type MiddlewareEvent = InternalEvent & {
3032
responseHeaders?: Record<string, string | string[]>;
3133
isExternalRewrite?: boolean;
@@ -95,8 +97,10 @@ export async function handleMiddleware(
9597
url,
9698
body: convertBodyToReadableStream(internalEvent.method, internalEvent.body),
9799
} as unknown as Request);
98-
// We want to normalize the Location header if it exists. Headers.has() doesn't care about the case of the header name.
99-
if (result.headers.has("Location")) {
100+
101+
const statusCode = result.status;
102+
// We want to normalize the Location header if its a redirect response. Headers.has() doesn't care about the case of the header name.
103+
if (REDIRECTS.has(statusCode) && result.headers.has("Location")) {
100104
result.headers.set(
101105
"Location",
102106
normalizeLocationHeader(
@@ -105,7 +109,6 @@ export async function handleMiddleware(
105109
),
106110
);
107111
}
108-
const statusCode = result.status;
109112

110113
/* Apply override headers from middleware
111114
NextResponse.next({

0 commit comments

Comments
 (0)