Skip to content

Commit 111fbda

Browse files
committed
feat: add support for next15 geolocation
1 parent c5aa622 commit 111fbda

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

.changeset/friendly-kangaroos-give.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+
feat: add support for Next15 geolocation

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

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ export async function handleMiddleware(
4646
internalEvent: InternalEvent,
4747
middlewareLoader: MiddlewareLoader = defaultMiddlewareLoader,
4848
): Promise<MiddlewareOutputEvent | InternalResult> {
49-
const { query } = internalEvent;
49+
// We bypass the middleware if the request is internal
50+
if (internalEvent.headers["x-isr"]) return internalEvent;
51+
5052
const normalizedPath = localizePath(internalEvent);
5153
// We only need the normalizedPath to check if the middleware should run
5254
const hasMatch = middleMatch.some((r) => r.test(normalizedPath));
5355
if (!hasMatch) return internalEvent;
54-
// We bypass the middleware if the request is internal
55-
if (internalEvent.headers["x-isr"]) return internalEvent;
5656

5757
// Retrieve the protocol:
5858
// - In lambda, the url only contains the rawPath and the query - default to https
@@ -64,20 +64,33 @@ export async function handleMiddleware(
6464
: "http://localhost:3000";
6565

6666
const initialUrl = new URL(normalizedPath, host);
67-
initialUrl.search = convertToQueryString(query);
67+
initialUrl.search = convertToQueryString(internalEvent.query);
6868
const url = initialUrl.toString();
6969

7070
const middleware = await middlewareLoader();
7171

72+
const geo = {
73+
city: internalEvent.headers["x-open-next-city"],
74+
country: internalEvent.headers["x-open-next-country"],
75+
region: internalEvent.headers["x-open-next-region"],
76+
latitude: internalEvent.headers["x-open-next-latitude"],
77+
longitude: internalEvent.headers["x-open-next-longitude"],
78+
};
79+
7280
const result: Response = await middleware.default({
73-
geo: {
74-
city: internalEvent.headers["x-open-next-city"],
75-
country: internalEvent.headers["x-open-next-country"],
76-
region: internalEvent.headers["x-open-next-region"],
77-
latitude: internalEvent.headers["x-open-next-latitude"],
78-
longitude: internalEvent.headers["x-open-next-longitude"],
81+
// `geo` is pre Next 15
82+
geo,
83+
headers: {
84+
...internalEvent.headers,
85+
// Next 15 geo headers
86+
// See https://github.com/vercel/vercel/blob/7714b1c/packages/functions/src/headers.ts
87+
"x-vercel-ip-city": geo.city,
88+
"x-vercel-ip-country": geo.country,
89+
"x-vercel-ip-country-region": geo.region,
90+
"x-vercel-ip-latitude": geo.latitude,
91+
"x-vercel-ip-longitude": geo.longitude,
7992
},
80-
headers: internalEvent.headers,
93+
8194
method: internalEvent.method || "GET",
8295
nextConfig: {
8396
basePath: NextConfig.basePath,

0 commit comments

Comments
 (0)