Skip to content

Commit

Permalink
breaking: remove geo and ip from NextRequest (#68379)
Browse files Browse the repository at this point in the history
### Overview 

This PR removes the `geo` and `ip` properties from `NextRequest` for
Next.js 15. These values had to be provided by your hosting provider.
Since this feature was not generally useful for the broader Next.js
community (outside of Vercel), we feel it makes more sense to keep this
as a Vercel-specific feature and not something built into the framework.

### Alternatives

If you are using Vercel, you can alternatively use the `geolocation` and
`ipAddress` functions from
[`@vercel/functions`](https://vercel.com/docs/functions/vercel-functions-package)
instead:

```js filename="middleware.ts"
import { geolocation, ipAddress } from '@vercel/functions';
import type { NextRequest } from 'next/server'

export function middleware(request: NextRequest) {
  const { city } = geolocation(request);
  const ip = ipAddress(request)

  // ...
}
```

---------

Co-authored-by: Eng Zer Jun <[email protected]>
  • Loading branch information
leerob and Juneezee authored Sep 12, 2024
1 parent 1710b3d commit 56dc3ed
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,31 @@ module.exports = nextConfig
Auto instrumentation for Speed Insights was removed in Next.js 15.

To continue using Speed Insights, follow the [Vercel Speed Insights Quickstart](https://vercel.com/docs/speed-insights/quickstart) guide.

## `NextRequest` Geolocation

The `geo` and `ip` properties on `NextRequest` have been removed. These values had to be provided by your hosting provider.

If you are using Vercel, you can alternatively use the `geolocation` and `ipAddress` functions from [`@vercel/functions](https://vercel.com/docs/functions/vercel-functions-package) instead:

```ts filename="middleware.ts"
import { geolocation } from '@vercel/functions'
import type { NextRequest } from 'next/server'

export function middleware(request: NextRequest) {
const { city } = geolocation(request)

// ...
}
```

```ts filename="middleware.ts"
import { ipAddress } from '@vercel/functions'
import type { NextRequest } from 'next/server'

export function middleware(request: NextRequest) {
const ip = ipAddress(request)

// ...
}
```
36 changes: 4 additions & 32 deletions docs/02-app/02-api-reference/04-functions/next-request.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -116,36 +116,8 @@ The following options are available:
</AppOnly>

## `ip`
## Version History

The `ip` property is a string that contains the IP address of the request. This value can optionally be provided by your hosting platform.

> **Good to know:** On [Vercel](https://vercel.com/docs/frameworks/nextjs?utm_source=next-site&utm_medium=docs&utm_campaign=next-website), this value is provided by default. On other platforms, you can use the [`X-Forwarded-For`](https://developer.mozilla.org/docs/Web/HTTP/Headers/X-Forwarded-For) header to provide the IP address.
```ts
// Provided by Vercel
request.ip
// Self-hosting
request.headers.get('X-Forwarded-For')
```

## `geo`

The `geo` property is an object that contains the geographic information of the request. This value can optionally be provided by your hosting platform.

> **Good to know:** On [Vercel](https://vercel.com/docs/frameworks/nextjs?utm_source=next-site&utm_medium=docs&utm_campaign=next-website), this value is provided by default. On other platforms, you can use the [`X-Forwarded-For`](https://developer.mozilla.org/docs/Web/HTTP/Headers/X-Forwarded-For) header to provide the IP address, then use a [third-party service](https://ip-api.com/) to lookup the geographic information.
```ts
// Provided by Vercel
request.geo.city
request.geo.country
request.geo.region
request.geo.latitude
request.geo.longitude

// Self-hosting
function getGeo(request) {
let ip = request.headers.get('X-Forwarded-For')
// Use a third-party service to lookup the geographic information
}
```
| Version | Changes |
| --------- | ----------------------- |
| `v15.0.0` | `ip` and `geo` removed. |
2 changes: 0 additions & 2 deletions packages/next/src/server/web/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,7 @@ export async function adapter(
input: stripInternalSearchParams(normalizeUrl, true).toString(),
init: {
body: params.request.body,
geo: params.request.geo,
headers: requestHeaders,
ip: params.request.ip,
method: params.request.method,
nextConfig: params.request.nextConfig,
signal: params.request.signal,
Expand Down
21 changes: 0 additions & 21 deletions packages/next/src/server/web/spec-extension/request.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { I18NConfig } from '../../config-shared'
import type { RequestData } from '../types'
import { NextURL } from '../next-url'
import { toNodeOutgoingHttpHeaders, validateURL } from '../utils'
import { RemovedUAError, RemovedPageError } from '../error'
Expand All @@ -15,8 +14,6 @@ export const INTERNALS = Symbol('internal request')
export class NextRequest extends Request {
[INTERNALS]: {
cookies: RequestCookies
geo: RequestData['geo']
ip?: string
url: string
nextUrl: NextURL
}
Expand All @@ -33,8 +30,6 @@ export class NextRequest extends Request {
})
this[INTERNALS] = {
cookies: new RequestCookies(this.headers),
geo: init.geo || {},
ip: init.ip,
nextUrl,
url: process.env.__NEXT_NO_MIDDLEWARE_URL_NORMALIZE
? url
Expand All @@ -45,8 +40,6 @@ export class NextRequest extends Request {
[Symbol.for('edge-runtime.inspect.custom')]() {
return {
cookies: this.cookies,
geo: this.geo,
ip: this.ip,
nextUrl: this.nextUrl,
url: this.url,
// rest of props come from Request
Expand All @@ -70,14 +63,6 @@ export class NextRequest extends Request {
return this[INTERNALS].cookies
}

public get geo() {
return this[INTERNALS].geo
}

public get ip() {
return this[INTERNALS].ip
}

public get nextUrl() {
return this[INTERNALS].nextUrl
}
Expand Down Expand Up @@ -106,12 +91,6 @@ export class NextRequest extends Request {
}

export interface RequestInit extends globalThis.RequestInit {
geo?: {
city?: string
country?: string
region?: string
}
ip?: string
nextConfig?: {
basePath?: string
i18n?: I18NConfig | null
Expand Down
8 changes: 0 additions & 8 deletions packages/next/src/server/web/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,7 @@ import type { FetchMetrics } from '../base-http'
export type { MiddlewareConfig } from '../../build/analysis/get-page-static-info'

export interface RequestData {
geo?: {
city?: string
country?: string
region?: string
latitude?: string
longitude?: string
}
headers: OutgoingHttpHeaders
ip?: string
method: string
nextConfig?: {
basePath?: string
Expand Down
3 changes: 0 additions & 3 deletions test/unit/web-runtime/next-request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ it('should allow the 2nd parameter to be undefined', () => {
const request = new NextRequest('https://vercel.com')
expectTypeOf(request).toMatchTypeOf<NextRequest>()

expect(
new NextRequest('https://vercel.com', { geo: { city: 'Mars' } })
).toHaveProperty('geo.city', 'Mars')
expect(new NextRequest('https://vercel.com')).toHaveProperty(
'nextUrl.pathname',
'/'
Expand Down

0 comments on commit 56dc3ed

Please sign in to comment.