Workers scripts can now pass redirect: "error" to fetch() or to a Request object. When the upstream response carries a redirect status (301, 302, 303, 307, or 308), the call fails with a TypeError and never follows the Location header, even if that header is missing. Non‑redirect 3xx responses such as 304 still resolve normally. This completes support for the three redirect modes—follow, manual, and error—required by the WHATWG Fetch Standard.
What Changed in Workers fetch
The runtime now recognises the redirect init field with the literal value "error". Previously Workers only offered follow (the default) and manual. With the new mode, any redirect response triggers an immediate rejection, and the script receives a TypeError instead of a Response object.
Why It Matters for Engineers
- Credential protection: Calls that include sensitive headers (e.g.,
Cookie,X-API-Key,Authorization) will not be forwarded to an unexpected location if the upstream service issues a redirect. - SSRF mitigation: After validating a user‑supplied URL, developers can block redirect hops to hosts that were not part of the original allow‑list.
- API contract enforcement: Unexpected redirects—such as an upstream service sending a login page—are surfaced as errors immediately, avoiding wasted sub‑requests and downstream parsing failures.
- Runtime parity: The same
redirect: "error"option can be used in Workers and in browser code, simplifying shared libraries.
Architectural and Operational Implications
Adopting the error mode introduces a new failure path that must be handled explicitly. Existing fetch calls that relied on automatic following will now throw; therefore, code should wrap the call in try / catch and translate the TypeError into an appropriate HTTP response or retry strategy. Monitoring should be updated to surface these TypeError events, as they may indicate mis‑configured upstream services rather than malicious activity.
From an operational perspective, the change does not affect the underlying edge network, but it does alter the observable request count. Because redirects are no longer followed, the total number of sub‑requests per transaction can drop, reducing latency and edge compute usage. Teams should benchmark critical paths after enabling the mode to confirm expected performance gains.
Security tooling that audits outbound requests can now treat a rejected redirect as a successful enforcement of a policy, rather than a silent pass‑through. Auditors may want to add a rule that flags any fetch without an explicit redirect setting, encouraging developers to opt‑in to the safest behavior.
Related CloudNinjas coverage: hands-on guides.
What This Means For Practitioners
Review existing Workers code that performs outbound HTTP calls. Add redirect: "error" wherever a request carries sensitive headers or where the target URL is user‑controlled. Update error handling to catch the resulting TypeError and return a meaningful response to callers. Finally, adjust monitoring dashboards to track the new error type and verify that legitimate redirects are being handled intentionally.
export default {
async fetch(request, env) {
try {
const response = await fetch("https://api.example.com/v1/token", {
method: "POST",
headers: { "X-API-Key": env.API_KEY },
redirect: "error",
});
return response;
} catch {
return new Response(
"Upstream request failed or returned an unexpected redirect",
{ status: 502 }
);
}
},
};
