Skip to content

Geo data missing in middleware #2880

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
cshawaus opened this issue Apr 29, 2025 · 2 comments
Closed

Geo data missing in middleware #2880

cshawaus opened this issue Apr 29, 2025 · 2 comments

Comments

@cshawaus
Copy link

cshawaus commented Apr 29, 2025

After upgrading to Next.js v15, I noticed the geo property on NextRequest was missing, which was fine because Vercel removed it from the interface.

However, I assume the object should still be injected into the request object via the edge function context. This assumption is based on:

const geo: RequestData['geo'] = {
city,
country: country?.code,
region: subdivision?.code,
latitude: latitude?.toString(),
longitude: longitude?.toString(),
timezone,
}

It returns null when accessing the geo property, like Next.js v14. That makes me believe that something within Next.js was removed to support this, but given that this plugin should reinstate it, I'm perplexed about the intended use.

Any thoughts would be much appreciated.

@mrstork
Copy link
Contributor

mrstork commented May 1, 2025

You are correct that this functionality was removed from Next.js in v15.0.0
(See: breaking: remove geo and ip from NextRequest: #68379)

Due to how geo was removed from Next.js, even though we are passing the values along, the framework doesn't pass these on to the middleware handlers as before. We are currently supporting multiple versions of Next.js, so the supporting code you linked continues to ensure we can support this feature in v13 and v14, though I completely understand the confusion this may have caused.

There is still a way to access the same geo values as before on a site that's deployed to Netlify, and that's via the Netlify-specific Context object.

Example:

export async function middleware(request: NextRequest) {
    // @ts-expect-error Netlify global
    return Response.json({ geo: Netlify.context.geo });
}

Hope that helps clear things up!

@cshawaus
Copy link
Author

cshawaus commented May 2, 2025

@mrstork, thanks for confirming the behaviour. I had completely forgotten that the global Netlify context was available within middleware. It would be good to have the documentation for both OpenNext and Netlify updated to call this out and avoid any confusion others may also have.

In any case, I have used the below code which works really well, especially for local testing outside of the Netlify CLI by hooking into the x-nf-country header.

type Geo = Exclude<(typeof Netlify)['context'], null>['geo']

let geo: Geo | undefined
try {
  geo = Netlify.context?.geo
} catch {}

const country =
  geo?.country?.code ??
  request.headers.get('x-nf-country')

@cshawaus cshawaus closed this as completed May 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants