Next-JS middleware ( NextRequest.nextUrl.locale ) return empty string in deployed

Viewed 14

I have a bug in next-js middleware

in middleware function is return a NextRequest pram in nextjs docs say:

The NextRequest object is an extension of the native Request interface, with the     
following added methods and properties:

- cookies - A Map with cookies from the Request. See Using cookies in Middleware
- nextUrl: Includes an extended, parsed, URL object that gives you access to Next.js 
- specific properties such as pathname, basePath, trailingSlash and i18n. Includes the 
- following properties:
- basePath (string)
- buildId (string || undefined)
- defaultLocale (string || undefined)
- domainLocale
  - defaultLocale: (string)
  - domain: (string)
  - http: (boolean || undefined)
  - locales: (string[] || undefined)
- locale (string || undefined)

thats mean I can access current locale from NextRequest.nextUrl.locale. good, this is working in localhost and already I get a locale.

but after deploy project in netlify and print NextRequest.nextUrl.locale in console thus

console.log({locale: NextRequest.nextUrl.locale});

is returned me

{ locale: "" }

meaning NextRequest.nextUrl.locale = "" & empty String

why this bug ??

thats my code

const middleware = async (req) => {
  if (
    req.nextUrl.pathname.startsWith("/_next") ||
    req.nextUrl.pathname.includes("/api/") ||
    PUBLIC_FILE.test(req.nextUrl.pathname)
  ) {
    return;
  }
  
  
  console.log({locale: req.nextUrl.locale});

  return;
};

export { middleware };

0 Answers
Related