Hi guys,
I couldn’t find information on how to get the geo in the middleware. For example, a user go on the website and I create a cookie location: Canada or location: city
For now I tried, but no success. It’s always USx:
export default async function middleware(req: NextRequest) {
const { nextUrl: url, geo } = req;
const city = geo?.latitude ?? 'USx';
const res = NextResponse.next();
const supabase = createMiddlewareSupabaseClient({ req, res });
res.cookies.set('city', city);
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
Enter your email to get $200 in credit for your first 60 days with DigitalOcean.
New accounts only. By submitting your email you agree to our Privacy Policy.
Hey @pob944,
I’ve not used NextJS however here is what I would do if I had to troubleshoot this and see why it doesn’t work :
Make sure that the
geo
property is being set correctly in thenextUrl
object. You can check this by logging thegeo
property to the console in your middleware function.Check that the user’s location can be determined using the
geo
property. You can do this by logging thelatitude
andlongitude
properties of thegeo
object to the console.If the user’s location cannot be determined, you can use a fallback location as a default value for the
city
variable. For example, you can setconst city = geo?.city ?? 'Canada';
to set thecity
variable to “Canada” if thegeo
object does not contain acity
property.Check that the cookie is being set correctly: Make sure that the cookie is being set correctly by logging the cookie value to the console or using a browser extension like “EditThisCookie” to inspect the cookie.
As said I’ve not used Nextjs however this should work either way:
Hope I help even a little bit!
Hey @pob944,
Make sure that the
geo
property is being set correctly in thenextUrl
object. You can check this by logging thegeo
property to the console in your middleware function.Check that the user’s location can be determined using the
geo
property. You can do this by logging thelatitude
andlongitude
properties of thegeo
object to the console.If the user’s location cannot be determined, you can use a fallback location as a default value for the
city
variable. For example, you can setconst city = geo?.city ?? 'Canada';
to set thecity
variable to “Canada” if thegeo
object does not contain acity
property.Check that the cookie is being set correctly: Make sure that the cookie is being set correctly by logging the cookie value to the console or using a browser extension like “EditThisCookie” to inspect the cookie.
I’ve not used Nextjs however this should work eitherway :
Hope I help even a little bit!
Hey @pob944,
Make sure that the
geo
property is being set correctly in thenextUrl
object. You can check this by logging thegeo
property to the console in your middleware function.Check that the user’s location can be determined using the
geo
property. You can do this by logging thelatitude
andlongitude
properties of thegeo
object to the console.If the user’s location cannot be determined, you can use a fallback location as a default value for the
city
variable. For example, you can setconst city = geo?.city ?? 'Canada';
to set thecity
variable to “Canada” if thegeo
object does not contain acity
property.Check that the cookie is being set correctly: Make sure that the cookie is being set correctly by logging the cookie value to the console or using a browser extension like “EditThisCookie” to inspect the cookie.
I’ve not used Nextjs however this should work eitherway :
Hope I help even a little bit!