Skip to content

Commit 784fb89

Browse files
authored
fix: Avoid merging Location header on response when its an array (#977)
1 parent 75b9d18 commit 784fb89

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

.changeset/seven-ducks-breathe.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@opennextjs/aws": patch
3+
---
4+
5+
fix: Avoid merging Location header on response when its an array

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type http from "node:http";
2+
import logger from "../logger";
23

34
export const parseHeaders = (
45
headers?: http.OutgoingHttpHeader[] | http.OutgoingHttpHeaders,
@@ -12,7 +13,27 @@ export const parseHeaders = (
1213
if (value === undefined) {
1314
continue;
1415
}
15-
result[key.toLowerCase()] = convertHeader(value);
16+
const keyLower = key.toLowerCase();
17+
/**
18+
* Next can return an Array for the Location header when you return null from a get in the cacheHandler on a page that has a redirect()
19+
* We dont want to merge that into a comma-separated string
20+
* If they are the same just return one of them
21+
* Otherwise return the last one
22+
* See: https://github.com/opennextjs/opennextjs-cloudflare/issues/875#issuecomment-3258248276
23+
* and https://github.com/opennextjs/opennextjs-aws/pull/977#issuecomment-3261763114
24+
*/
25+
if (keyLower === "location" && Array.isArray(value)) {
26+
if (value[0] === value[1]) {
27+
result[keyLower] = value[0];
28+
} else {
29+
logger.warn(
30+
"Multiple different values for Location header found. Using the last one",
31+
);
32+
result[keyLower] = value[value.length - 1];
33+
}
34+
continue;
35+
}
36+
result[keyLower] = convertHeader(value);
1637
}
1738

1839
return result;

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

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

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

0 commit comments

Comments
 (0)