Starting with workers and Lat/Long

Just started using Cloudflare and liking the testing that we are doing. We are working to get Latitude and Longitude into the header similar to how we are doing that with a competing CDN.

I have been reading the articles below and having some issues just getting started with workers and the general setup. Any assistance would be appreciated.

  1. Get wrangler installed and working
  2. Install VSCode and get comfortable with it
  3. Create a new project using wrangler generate ...
  4. Make a test site or use the .dev endpoint and start playing with it, using wrangler publish, wrangler tail or wrangler dev.
  5. Learn js, particularly don’t get bit by forgetting to use await on asynchronous calls.
  6. Look at all the examples in the docs :slight_smile:
1 Like

Got this all working! Here is the code for anyone since there are 75 views.

addEventListener(“fetch”, event => {
event.respondWith(fetchAndReplace(event.request))
})

async function fetchAndReplace(request){
// Set newRequestHeaders to current Headers
let newRequestHeaders = new Headers(request.headers)
const country = request.headers.get(“cf-ipcountry”)
const latitude = request.cf.latitude
const longitude = request.cf.longitude
newRequestHeaders.append(“MB-UserIPGeoLocation”, ${country} + “;” + ${latitude} + “,” + ${longitude})

//Fetch from origin server.
let response = await fetch(request, {headers: newRequestHeaders})

    return response

}

1 Like

Thanks for the assist David.

1 Like

You should mention that you are on a Business plan, as otherwise you wouldn’t get that to work.