Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/many-elephants-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@opennextjs/aws": patch
---

fix city name header encoding

- encode the header in cloudflare wrapper
- decode the header in the routing layer
4 changes: 3 additions & 1 deletion packages/open-next/src/core/routing/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ export async function handleMiddleware(
const result: Response = await middleware.default({
// `geo` is pre Next 15.
geo: {
city: headers["x-open-next-city"],
// The city name is percent-encoded.
// See https://github.com/vercel/vercel/blob/4cb6143/packages/functions/src/headers.ts#L94C19-L94C37
city: decodeURIComponent(headers["x-open-next-city"]),
country: headers["x-open-next-country"],
region: headers["x-open-next-region"],
latitude: headers["x-open-next-latitude"],
Expand Down
20 changes: 14 additions & 6 deletions packages/open-next/src/overrides/wrappers/cloudflare-edge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ import type {
} from "types/open-next";
import type { Wrapper, WrapperHandler } from "types/overrides";

const cfPropNameToHeaderName = {
city: "x-open-next-city",
const cfPropNameMapping: Record<
string,
string | [(s: string) => string, string]
> = {
// The city name is percent-encoded.
// See https://github.com/vercel/vercel/blob/4cb6143/packages/functions/src/headers.ts#L94C19-L94C37
city: [encodeURIComponent, "x-open-next-city"],
country: "x-open-next-country",
regionCode: "x-open-next-region",
latitude: "x-open-next-latitude",
Expand Down Expand Up @@ -46,12 +51,15 @@ const handler: WrapperHandler<
const cfProperties = (request as any).cf as
| Record<string, string | null>
| undefined;
for (const [propName, headerName] of Object.entries(
cfPropNameToHeaderName,
)) {
for (const [propName, mapping] of Object.entries(cfPropNameMapping)) {
const propValue = cfProperties?.[propName];
if (propValue != null) {
internalEvent.headers[headerName] = propValue;
const [encode, headerName] = Array.isArray(mapping)
? mapping
: [null, mapping];
internalEvent.headers[headerName] = encode
? encode(propValue)
: propValue;
}
}

Expand Down
Loading