@@ -46,13 +46,13 @@ export async function handleMiddleware(
46
46
internalEvent : InternalEvent ,
47
47
middlewareLoader : MiddlewareLoader = defaultMiddlewareLoader ,
48
48
) : 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
+
50
52
const normalizedPath = localizePath ( internalEvent ) ;
51
53
// We only need the normalizedPath to check if the middleware should run
52
54
const hasMatch = middleMatch . some ( ( r ) => r . test ( normalizedPath ) ) ;
53
55
if ( ! hasMatch ) return internalEvent ;
54
- // We bypass the middleware if the request is internal
55
- if ( internalEvent . headers [ "x-isr" ] ) return internalEvent ;
56
56
57
57
// Retrieve the protocol:
58
58
// - In lambda, the url only contains the rawPath and the query - default to https
@@ -64,20 +64,33 @@ export async function handleMiddleware(
64
64
: "http://localhost:3000" ;
65
65
66
66
const initialUrl = new URL ( normalizedPath , host ) ;
67
- initialUrl . search = convertToQueryString ( query ) ;
67
+ initialUrl . search = convertToQueryString ( internalEvent . query ) ;
68
68
const url = initialUrl . toString ( ) ;
69
69
70
70
const middleware = await middlewareLoader ( ) ;
71
71
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
+
72
80
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 ,
79
92
} ,
80
- headers : internalEvent . headers ,
93
+
81
94
method : internalEvent . method || "GET" ,
82
95
nextConfig : {
83
96
basePath : NextConfig . basePath ,
0 commit comments