Skip to content

Commit 129a1d1

Browse files
committed
fix: add longitude, latitude to RequestData.geo
1 parent 915e9dd commit 129a1d1

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

cypress/integration/middleware/enhanced.spec.ts

+12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { NextRequest } from 'next/server'
12
describe('Enhanced middleware', () => {
23
it('adds request headers', () => {
34
cy.request('/api/hello').then((response) => {
@@ -25,4 +26,15 @@ describe('Enhanced middleware', () => {
2526
.that.includes('This was static but has been transformed in')
2627
})
2728
})
29+
30+
it('adds geo data', () => {
31+
cy.request('/api/geo-test').then((response) => {
32+
expect(response.body).to.have.nested.property('headers.x-geo-country')
33+
expect(response.body).to.have.nested.property('headers.x-geo-region')
34+
expect(response.body).to.have.nested.property('headers.x-geo-city')
35+
expect(response.body).to.have.nested.property('headers.x-geo-longitude')
36+
expect(response.body).to.have.nested.property('headers.x-geo-latitude')
37+
expect(response.body).to.have.nested.property('headers.x-geo-timezone')
38+
})
39+
})
2840
})

demos/middleware/middleware.ts

+10
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ export async function middleware(req: NextRequest) {
2929
return request.next()
3030
}
3131

32+
if (pathname.startsWith('/api/geo')) {
33+
req.headers.set('x-geo-country', req.geo.country)
34+
req.headers.set('x-geo-region', req.geo.region)
35+
req.headers.set('x-geo-city', req.geo.city)
36+
req.headers.set('x-geo-longitude', req.geo.longitude)
37+
req.headers.set('x-geo-latitude', req.geo.latitude)
38+
39+
return request.next()
40+
}
41+
3242
if (pathname.startsWith('/headers')) {
3343
// Add a header to the rewritten request
3444
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+
}

0 commit comments

Comments
 (0)