Skip to content

Commit 3f35549

Browse files
authored
fix: add longitude, latitude, and timezone to RequestData.geo (#1777)
1 parent b85f2a2 commit 3f35549

File tree

7 files changed

+48
-6
lines changed

7 files changed

+48
-6
lines changed

cypress/integration/middleware/enhanced.spec.ts

+11
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,15 @@ describe('Enhanced middleware', () => {
2525
.that.includes('This was static but has been transformed in')
2626
})
2727
})
28+
29+
it('adds geo data', () => {
30+
cy.request('/api/geo').then((response) => {
31+
expect(response.body).to.have.nested.property('headers.x-geo-country')
32+
expect(response.body).to.have.nested.property('headers.x-geo-region')
33+
expect(response.body).to.have.nested.property('headers.x-geo-city')
34+
expect(response.body).to.have.nested.property('headers.x-geo-longitude')
35+
expect(response.body).to.have.nested.property('headers.x-geo-latitude')
36+
expect(response.body).to.have.nested.property('headers.x-geo-timezone')
37+
})
38+
})
2839
})

demos/middleware/middleware.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { NextResponse } from 'next/server'
2-
import type { NextRequest } from 'next/server'
3-
4-
import { MiddlewareRequest } from '@netlify/next'
2+
import { MiddlewareRequest, NextRequest } from '@netlify/next'
53

64
export async function middleware(req: NextRequest) {
75
let response
@@ -29,6 +27,17 @@ export async function middleware(req: NextRequest) {
2927
return request.next()
3028
}
3129

30+
if (pathname.startsWith('/api/geo')) {
31+
req.headers.set('x-geo-country', req.geo.country)
32+
req.headers.set('x-geo-region', req.geo.region)
33+
req.headers.set('x-geo-city', req.geo.city)
34+
req.headers.set('x-geo-longitude', req.geo.longitude)
35+
req.headers.set('x-geo-latitude', req.geo.latitude)
36+
req.headers.set('x-geo-timezone', req.geo.timezone)
37+
38+
return request.next()
39+
}
40+
3241
if (pathname.startsWith('/headers')) {
3342
// Add a header to the rewritten request
3443
req.headers.set('x-hello', 'world')

demos/middleware/pages/api/geo.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function handler(req, res) {
2+
res.status(200).json({ name: 'geo-test', headers: req.headers })
3+
}

packages/next/src/middleware/request.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
import type { NextURL } from 'next/dist/server/web/next-url'
22
import { NextResponse } from 'next/server'
3-
import type { NextRequest } from 'next/server'
3+
import type { NextRequest as InternalNextRequest } from 'next/server'
44

55
import { MiddlewareResponse } from './response'
66

7+
export type NextRequest = InternalNextRequest & {
8+
get geo(): {
9+
timezone?: string
10+
}
11+
}
12+
713
export interface NextOptions {
814
/**
915
* Include conditional request headers in the request to the origin.

packages/next/test/request.spec.ts

+6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ describe('MiddlewareRequest', () => {
2525
code: chance.province(),
2626
},
2727
city: chance.city(),
28+
latitude: chance.latitude(),
29+
longitude: chance.longitude(),
30+
timezone: chance.timezone(),
2831
},
2932
ip,
3033
}
@@ -33,6 +36,9 @@ describe('MiddlewareRequest', () => {
3336
country: context.geo.country?.code,
3437
region: context.geo.subdivision?.code,
3538
city: context.geo.city,
39+
latitude: context.geo.latitude?.toString(),
40+
longitude: context.geo.longitude?.toString(),
41+
timezone: context.geo.timezone,
3642
}
3743

3844
const req = new URL(url)

packages/runtime/src/templates/edge/next-dev.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,14 @@ const handler = async (req, context) => {
4343
return
4444
}
4545

46-
// This is the format expected by Next.js
46+
// This is the format expected by Next.js along with the timezone which we support.
4747
const geo = {
4848
country: context.geo.country?.code,
4949
region: context.geo.subdivision?.code,
5050
city: context.geo.city,
51+
latitude: context.geo.latitude?.toString(),
52+
longitude: context.geo.longitude?.toString(),
53+
timezone: context.geo.timezone,
5154
}
5255

5356
// A default request id is fine locally

packages/runtime/src/templates/edge/runtime.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export interface RequestData {
2020
region?: string
2121
latitude?: string
2222
longitude?: string
23+
timezone?: string
2324
}
2425
headers: Record<string, string>
2526
ip?: string
@@ -63,10 +64,13 @@ const handler = async (req: Request, context: Context) => {
6364
return
6465
}
6566

66-
const geo = {
67+
const geo: RequestData['geo'] = {
6768
country: context.geo.country?.code,
6869
region: context.geo.subdivision?.code,
6970
city: context.geo.city,
71+
latitude: context.geo.latitude?.toString(),
72+
longitude: context.geo.longitude?.toString(),
73+
timezone: context.geo.timezone,
7074
}
7175

7276
const requestId = req.headers.get('x-nf-request-id')

0 commit comments

Comments
 (0)