Worker too many redirects

Hello,

// Configuration to map country codes to regional stores
const regionalStores = new Map([
    ["ES", "site.es"],
    ["INT", "site.com"],
  ]);
  
  
  addEventListener("fetch", event => {
    event.respondWith(handleRequest(event.request));
  });
  
  async function handleRequest(request) {
    // Read the IP address from Cloudflare
    const country = request.headers.get("cf-ipcountry")
    console.log(country)
    
    // Set our default regional store
    // This is used if there's no matching store configuration
    let regionalStore = regionalStores.get("INT");

  
    // Set the new regional store, if it's set in the regionalStores map
    if (regionalStores.has(country)) {
      regionalStore = regionalStores.get(country);
    } 
    
    // Redirect the user to the new URL
    return Response.redirect("https://" + regionalStore);
  }

I am running this code to redirect users based on their location, however when the user matches “INT” case, and i visit the site the site shows an error that it has too many redirects, but for the other case it works fine when i change my IP and visit the site.

any ideas whats wrong?

As usual, it would be handy to have the domain…

Where is this worker deployed, domain and route?

I can certainly imagine a situation where you have deployed this worker to site.com, where it matches… directs me to site.com, where it matches… redirects me to site.com, etc, etc.

robert

1 Like