microsoft/playwright · error · Error

Found redirect cycle for ${url}

Error message

Found redirect cycle for ${url}

What it means

Thrown by HarBackend._harFindResponse() when following redirect chains in a HAR file and the same HAR entry is visited more than once. The method maintains a visited Set of entries; encountering a previously-visited entry indicates a circular redirect chain in the HAR data, which would otherwise cause an infinite loop.

Source

Thrown at packages/playwright-core/src/server/harBackend.ts:138

      if (!entries.length)
        return;

      let entry = entries[0];

      // Disambiguate using headers - then one with most matching headers wins.
      if (entries.length > 1) {
        const list: { candidate: har.Entry, matchingHeaders: number }[] = [];
        for (const candidate of entries) {
          const matchingHeaders = countMatchingHeaders(candidate.request.headers, headers);
          list.push({ candidate, matchingHeaders });
        }
        list.sort((a, b) => b.matchingHeaders - a.matchingHeaders);
        entry = list[0].candidate;
      }

      if (visited.has(entry))
        throw new Error(`Found redirect cycle for ${url}`);

      visited.add(entry);

      // Follow redirects.
      const locationHeader = entry.response.headers.find(h => h.name.toLowerCase() === 'location');
      if (redirectStatus.includes(entry.response.status) && locationHeader) {
        const locationURL = new URL(locationHeader.value, url);
        url = locationURL.toString();
        if ((entry.response.status === 301 || entry.response.status === 302) && method === 'POST' ||
          entry.response.status === 303 && !['GET', 'HEAD'].includes(method)) {
          // HTTP-redirect fetch step 13 (https://fetch.spec.whatwg.org/#http-redirect-fetch)
          method = 'GET';
        }
        continue;
      }

      return entry;
    }

View on GitHub (pinned to c8fc3bf8d3)

Solutions

  1. Re-record the HAR file to capture a correct, non-circular redirect chain.
  2. Inspect the HAR entries for the URL in the error message and fix any circular Location headers.
  3. Use the { update: true } option in routeFromHAR to fall back to live network for unmatched requests.
  4. If the redirect loop exists in the live site, fix the server-side redirect configuration.
Defensive patterns

Strategy: try-catch

Try / catch

try {
  await page.routeFromHAR(harPath);
} catch (e) {
  if (e.message.startsWith('Found redirect cycle')) {
    // Fall back to live network or re-record HAR
    await page.routeFromHAR(harPath, { update: true });
  } else throw e;
}

Prevention

When it happens

Trigger: Replaying a HAR file via routeFromHAR() where the recorded redirect chain is circular (e.g., URL A redirects to B, B redirects back to A). The HAR contains entries where following Location headers leads back to an already-visited entry. This can also occur with misrecorded HARs where multiple entries share the same URL and method, causing the disambiguation logic to loop.

Common situations: HAR recorded from a site with a redirect loop bug. HAR file is stale and the recorded redirect chain no longer matches the live site's behavior, causing entries to be revisited. Manually crafted or edited HAR with inconsistent redirect targets. Multiple HAR entries for the same URL where redirect following creates a cycle.

Related errors


AI-assisted analysis of microsoft/playwright@c8fc3bf8d3 (2026-08-12). Data as JSON: /api/errors/7efd84ac41db8f62. Report an issue: GitHub.