Workers aren't working, not providing cloudflare properties like country and timezone of the visitor

Hi,

Workers aren’t working anymore. I’ve added a piece of code to fetch the visitor’s country and Timezone but it’s not providing anymore. Could you pls help.

addEventListener("fetch", (event) => {
  event.respondWith(addHeaders(event.request))
})

async function addHeaders(request) {
  request = new Request(request)

  if ((cf = request.cf)) {
    request.headers.set("X-Worker-Country", cf.country)
    request.headers.set("X-Worker-Region", cf.regionCode)
    request.headers.set("X-Worker-Timezone", cf.timezone)
  }

  let response = await fetch(request)

  return response
}

When you say not working, is it erroring out or not adding the headers?

What does this part do?

  if ((cf = request.cf)) {
    request.headers.set("X-Worker-Country", cf.country)
    request.headers.set("X-Worker-Region", cf.regionCode)
    request.headers.set("X-Worker-Timezone", cf.timezone)
  }

It looks like you are doing an if statement when defining a variable? Do you mean to have something like this?

  if (request.cf) {
    const cf = request.cf
    request.headers.set("X-Worker-Country", cf.country)
    request.headers.set("X-Worker-Region", cf.regionCode)
    request.headers.set("X-Worker-Timezone", cf.timezone)
  }

So it’s not adding these values to the headers and yes that is assignment

It’s adding values to the header but not showing in the Network tab. Hence closing this thread.