Skip to content

Commit f7cab1a

Browse files
committed
fix: Avoid merging Location header on response when its an array
1 parent ba02f6b commit f7cab1a

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

packages/open-next/src/http/util.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ export const parseHeaders = (
1212
if (value === undefined) {
1313
continue;
1414
}
15+
// Next can sometimes return an Array for the Location header
16+
// See: https://github.com/opennextjs/opennextjs-cloudflare/issues/875#issuecomment-3258248276
17+
if (key.toLowerCase() === "location" && Array.isArray(value)) {
18+
result[key.toLowerCase()] = value[0];
19+
continue;
20+
}
1521
result[key.toLowerCase()] = convertHeader(value);
1622
}
1723

packages/tests-unit/tests/http/utils.test.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { parseSetCookieHeader } from "@opennextjs/aws/http/util.js";
1+
import type http from "node:http";
2+
3+
import { parseHeaders, parseSetCookieHeader } from "@opennextjs/aws/http/util.js";
24

35
describe("parseSetCookieHeader", () => {
46
it("returns an empty list if cookies is emptyish", () => {
@@ -43,3 +45,21 @@ describe("parseSetCookieHeader", () => {
4345
]);
4446
});
4547
});
48+
49+
describe("parseHeaders", () => {
50+
it("parses headers correctly", () => {
51+
const headers = parseHeaders({
52+
location: ["/target", "/target"],
53+
"x-custom-header": "customValue",
54+
"x-multiple-values": ["value1", "value2"],
55+
"x-undefined-header": undefined,
56+
"x-opennext": "is-so-cool",
57+
} as unknown as http.OutgoingHttpHeaders);
58+
expect(headers).toEqual({
59+
location: "/target",
60+
"x-custom-header": "customValue",
61+
"x-multiple-values": "value1,value2",
62+
"x-opennext": "is-so-cool",
63+
});
64+
});
65+
});

0 commit comments

Comments
 (0)