Skip to content

Commit aa59d03

Browse files
committed
chore: added timezone to geo data
1 parent 223937f commit aa59d03

File tree

5 files changed

+22
-2
lines changed

5 files changed

+22
-2
lines changed

cypress/integration/middleware/enhanced.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ describe('Enhanced middleware', () => {
3434
expect(response.body).to.have.nested.property('headers.x-geo-city')
3535
expect(response.body).to.have.nested.property('headers.x-geo-longitude')
3636
expect(response.body).to.have.nested.property('headers.x-geo-latitude')
37+
expect(response.body).to.have.nested.property('headers.x-geo-timezone')
3738
})
3839
})
3940
})

demos/middleware/middleware.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export async function middleware(req: NextRequest) {
3535
req.headers.set('x-geo-city', req.geo.city)
3636
req.headers.set('x-geo-longitude', req.geo.longitude)
3737
req.headers.set('x-geo-latitude', req.geo.latitude)
38+
req.headers.set('x-geo-timezone', req.geo.timezone)
3839

3940
return request.next()
4041
}

packages/next/src/middleware/request.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
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+
| {
10+
city?: string
11+
country?: string
12+
region?: string
13+
latitude?: string
14+
longitude?: string
15+
timezone?: string
16+
}
17+
| undefined
18+
}
19+
720
export interface NextOptions {
821
/**
922
* Include conditional request headers in the request to the origin.

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

Lines changed: 4 additions & 1 deletion
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

Lines changed: 2 additions & 0 deletions
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
@@ -69,6 +70,7 @@ const handler = async (req: Request, context: Context) => {
6970
city: context.geo.city,
7071
latitude: context.geo.latitude?.toString(),
7172
longitude: context.geo.longitude?.toString(),
73+
timezone: context.geo.timezone,
7274
}
7375

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

0 commit comments

Comments
 (0)