Skip to content

Commit 86916bf

Browse files
authored
fix the encoding of the geo city header (#688)
* fix(cloudflare): percent encode the x-open-next-city header fixes #687 * fix(middleware): decode the city header
1 parent c333860 commit 86916bf

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

.changeset/many-elephants-sing.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@opennextjs/aws": patch
3+
---
4+
5+
fix city name header encoding
6+
7+
- encode the header in cloudflare wrapper
8+
- decode the header in the routing layer

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ export async function handleMiddleware(
7474
const result: Response = await middleware.default({
7575
// `geo` is pre Next 15.
7676
geo: {
77-
city: headers["x-open-next-city"],
77+
// The city name is percent-encoded.
78+
// See https://github.com/vercel/vercel/blob/4cb6143/packages/functions/src/headers.ts#L94C19-L94C37
79+
city: decodeURIComponent(headers["x-open-next-city"]),
7880
country: headers["x-open-next-country"],
7981
region: headers["x-open-next-region"],
8082
latitude: headers["x-open-next-latitude"],

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)