Skip to content

Commit 198d2c9

Browse files
committed
fix(cloudflare): percent encode the x-open-next-city header
fixes #687
1 parent c333860 commit 198d2c9

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

packages/open-next/src/overrides/wrappers/cloudflare-edge.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@ import type {
55
} from "types/open-next";
66
import type { Wrapper, WrapperHandler } from "types/overrides";
77

8-
const cfPropNameToHeaderName = {
9-
city: "x-open-next-city",
8+
const cfPropNameMapping: Record<
9+
string,
10+
string | [(s: string) => string, string]
11+
> = {
12+
// The city name is percent-encoded.
13+
// See https://github.com/vercel/vercel/blob/4cb6143/packages/functions/src/headers.ts#L94C19-L94C37
14+
city: [encodeURIComponent, "x-open-next-city"],
1015
country: "x-open-next-country",
1116
regionCode: "x-open-next-region",
1217
latitude: "x-open-next-latitude",
@@ -46,12 +51,15 @@ const handler: WrapperHandler<
4651
const cfProperties = (request as any).cf as
4752
| Record<string, string | null>
4853
| undefined;
49-
for (const [propName, headerName] of Object.entries(
50-
cfPropNameToHeaderName,
51-
)) {
54+
for (const [propName, mapping] of Object.entries(cfPropNameMapping)) {
5255
const propValue = cfProperties?.[propName];
5356
if (propValue != null) {
54-
internalEvent.headers[headerName] = propValue;
57+
const [encode, headerName] = Array.isArray(mapping)
58+
? mapping
59+
: [null, mapping];
60+
internalEvent.headers[headerName] = encode
61+
? encode(propValue)
62+
: propValue;
5563
}
5664
}
5765

0 commit comments

Comments
 (0)