By pob944
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!
Hey @pob944,
Make sure that the geo property is being set correctly in the nextUrl object. You can check this by logging the geo 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 the latitude and longitude properties of the geo 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 set const city = geo?.city ?? 'Canada'; to set the city variable to “Canada” if the geo object does not contain a city 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 :
export default async function middleware(req: NextRequest) {
const { nextUrl: { geo } } = req;
const city = geo?.city ?? 'Canada';
const res = NextResponse.next();
res.cookies.set('city', city);
return { req, res };
}
Hope I help even a little bit!
Hey @pob944,
Make sure that the geo property is being set correctly in the nextUrl object. You can check this by logging the geo 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 the latitude and longitude properties of the geo 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 set const city = geo?.city ?? 'Canada'; to set the city variable to “Canada” if the geo object does not contain a city 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 :
export default async function middleware(req: NextRequest) {
const { nextUrl: { geo } } = req;
const city = geo?.city ?? 'Canada';
const res = NextResponse.next();
res.cookies.set('city', city);
return { req, res };
}
Hope I help even a little bit!
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 the nextUrl object. You can check this by logging the geo 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 the latitude and longitude properties of the geo 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 set const city = geo?.city ?? 'Canada'; to set the city variable to “Canada” if the geo object does not contain a city 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:
export default async function middleware(req: NextRequest) {
const { nextUrl: { geo } } = req;
const city = geo?.city ?? 'Canada';
const res = NextResponse.next();
res.cookies.set('city', city);
return { req, res };
}
Hope I help even a little bit!
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.