Skip to content

edge function geo is not working (undefined values) #24

@marcofranssen

Description

@marcofranssen

Middleware in my api folder.

import { NextResponse } from "next/server";

export async function middleware(req) {
  const { nextUrl: url, geo } = req;

  console.log(url);
  console.log(geo);

  if (url.pathname === "/api/ip-info") {
    const country = geo.country || "";
    const region = geo.region || "";

    url.searchParams.set("countryCode", country);
    url.searchParams.set("region", region);

    return NextResponse.rewrite(url);
  }

  return NextResponse.next();
}

Logs from Vercel.

/api/ip-info

{ city: undefined,
  country: undefined,
  region: undefined,
  latitude: undefined,
  longitude: undefined }

What am I missing?

The request is triggered via the CookieConsent consentTypeRequired function.

    <>
      <Head>
        <meta
          name="viewport"
          content="minimum-scale=1, initial-scale=1, maximum-scale=5, width=device-width, shrink-to-fit=no, viewport-fit=cover"
        />
      </Head>
      <DefaultSeo {...SEO} />
      <CookieConsentProvider consentTypeRequired={consentTypeRequired}>
        <Component {...pageProps} />
        <CookieConsent />
      </CookieConsentProvider>
    </>

Is there anything I could simplify? Currently I have a fallback in the ip-info api that uses external services to determine the geo. However that causes initial delays.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions