Next.js Discord

Discord Forum

Error reading request headers in Route handlers and Server Actions

Unanswered
Cape lion posted this in #help-forum
Open in Discord
Cape lionOP
hi y'all! does anyone know if is intended nextjs removes headers on production/deployments accessing through the headers() method in server actions or api routes such as cookie , x-invoke-path , or accept-language , or if might be only a problem related to Vercel deployments?

I have the following methods:

export const getRequestUrl = () => {
  const headerList = headers()
  const origin = headerList.get('origin') ?? ''
  const path = headerList.get('x-invoke-path') ?? ''
  return new URL(path, origin).toString()
}

export const getRequestCookie = () => headers().get('Cookie')

export function getRequestLocales(): string[] | undefined {
  const headersList = headers()
  const acceptLanguage = headersList.get('Accept-Language')

  if (!acceptLanguage) return undefined

  const locales = parseAcceptLanguage(acceptLanguage, {
    validate: void Intl.DateTimeFormat.supportedLocalesOf,
    ignoreWildcard: true
  })

  if (locales.length === 0) return undefined
  return locales
}


these headers/methods are resolving properly in development and also if i build & start in my local computer but once I deploy it to Vercel they are always undefined so I'm wondering if that's expected or could be a problem only with Vercel deployments. Also if there's a better or documented way to access those headers in api routes or server actions

0 Replies