Skip to content
Visit Workers on GitHub
Set theme to dark (⇧+D)

Country code redirect

Redirect a response based on the country code in the header of a visitor.

/** * Returns a redirect determined by the country code * @param {Request} request */async function redirect(request) {  // The `cf-ipcountry` header is not supported in the preview  const country = request.headers.get("cf-ipcountry")
  if (country != null && country in countryMap) {    const url = countryMap[country]    return Response.redirect(url)  }  else {    return await fetch(request)  }}/** * A map of the URLs to redirect to * @param {Object} countryMap */const countryMap = {  US: "",  EU: "",}
async function handleRequest(request) {  return redirect(request)}
addEventListener("fetch", event => {  event.respondWith(handleRequest(event.request))})