From 4ea3eec98cc8cb194d43f984a57c133ad2a95556 Mon Sep 17 00:00:00 2001 From: Mike Cao Date: Tue, 25 Apr 2023 16:29:47 -0700 Subject: [PATCH] Fixed vercel region issues. --- lib/detect.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/detect.ts b/lib/detect.ts index 01e6c9a6..868d0a80 100644 --- a/lib/detect.ts +++ b/lib/detect.ts @@ -63,10 +63,15 @@ export async function getLocation(ip, req) { } if (req.headers['x-vercel-ip-country']) { + const country = req.headers['x-vercel-ip-country']; + const region = req.headers['x-vercel-ip-country-region']; + const city = req.headers['x-vercel-ip-city']; + return { - country: req.headers['x-vercel-ip-country'], - subdivision1: req.headers['x-vercel-ip-country-region'], - city: decodeURIComponent(req.headers['x-vercel-ip-city']), + country, + // Vercel documentation says they return only region, but that is not always the case + subdivision1: region.includes('-') ? region : `${country}-${region}`, + city: decodeURIComponent(city), }; }