Worker too many redirects


// Configuration to map country codes to regional stores
const regionalStores = new Map([
    ["ES", ""],
    ["INT", ""],
  addEventListener("fetch", event => {
  async function handleRequest(request) {
    // Read the IP address from Cloudflare
    const country = request.headers.get("cf-ipcountry")
    // 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, where it matches… directs me to, where it matches… redirects me to, etc, etc.


1 Like