You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+51Lines changed: 51 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -247,6 +247,57 @@ function handler(event) {
247
247
248
248
The server function would then sets the `host` header of the request to the value of the `x-forwarded-host` header when sending the request to the `NextServer`.
249
249
250
+
#### WORKAROUND: Set `NextRequest` geolocation data
251
+
252
+
When your application is hosted on Vercel, you can access a user's geolocation inside your middleware through the `NextRequest` object.
253
+
254
+
```ts
255
+
exportfunction middleware(request:NextRequest) {
256
+
request.geo.country;
257
+
request.geo.city;
258
+
}
259
+
```
260
+
261
+
When your application is hosted on AWS, you can [obtain the geolocation data from CloudFront request headers](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/adding-cloudfront-headers.html#cloudfront-headers-viewer-location). However, there is no way to set this data on the `NextRequest` object passed to the middleware function.
262
+
263
+
To work around the issue, the `NextRequest` constructor is modified to initialize geolocation data from CloudFront headers, instead of using the default empty object.
CloudFront provides more detailed geolocation information, such as postal code and timezone. Here is a complete list of `geo` properties available in your middleware:
282
+
283
+
```ts
284
+
exportfunction middleware(request:NextRequest) {
285
+
// Supported by Next.js
286
+
request.geo.country;
287
+
request.geo.region;
288
+
request.geo.city;
289
+
request.geo.latitude;
290
+
request.geo.longitude;
291
+
292
+
// Also supported by OpenNext
293
+
request.geo.countryName;
294
+
request.geo.regionName;
295
+
request.geo.postalCode;
296
+
request.geo.timeZone;
297
+
request.geo.metroCode;
298
+
}
299
+
```
300
+
250
301
#### WORKAROUND: `NextServer` does not set cache response headers for HTML pages
251
302
252
303
As mentioned in the [Server function](#server-lambda-function) section, the server function uses the `NextServer` class from Next.js' build output to handle requests. However, `NextServer` does not seem to set the correct `Cache Control` headers.
0 commit comments