0

I manage to get the ip using event.getClientAddress() but I need user's country or country code so I can ssr the site based on user location. I am using netlify by the way for hosting.

1 Answers1

1

You can use an API from some service like:

These services do the following: source

Typically an Internet Service Provider (ISP) will have a particular range of IP addresses that they work with. These ISPs can delegate these different IP addresses to the various users and devices as they connect to the internet. IP geolocation works by looking up a particular IP against the IP ranges owned by particular ISP’s in a given area. This method is similar to how determining the carrier of a particular phone number works in the Abstract Phone Validation API, for example.

You can use JavaScript methods which depend upon browser API support and might not work for all users:

Intl.DisplayNames source

let regionNames = new Intl.DisplayNames(['en'], {type: 'region'});
regionNames.of('US');  // "United States"

window.navigator.userLanguage

const country = window.navigator.userLanguage || window.navigator.language.split("-", 2)[1]
// outputs US
Justin
  • 23
  • 6