Skip to content

Fetch not following redirects #297

Description

@MendyBerger

Fetch doesn't follow redirect responses even when explicitly set to follow.

Given the following JS

const STATUS_CODE = 302;
const EXPECTED_LOCATION = "https://example.com";
const URL = `https://httpbin.org/redirect-to?url=${EXPECTED_LOCATION}&status_code=${STATUS_CODE}`;

addEventListener('fetch', async e => {
    let promise = fetch(URL, {
        redirect: "follow",
    })
        .then((response) => {
            if (response.status !== STATUS_CODE)
                throw new Error(`Expected status code ${STATUS_CODE}, got ${response.status}`);
            const location = response.headers.get("Location");
            if (location !== EXPECTED_LOCATION)
                throw new Error(`Expected location ${EXPECTED_LOCATION}, got ${location}`);
            return response.text();
        })
        .then((text) => {
            if (!text.includes("Example Domain")) {
                throw new Error("Didn't redirect to example.com");
            }
        });
    e.waitUntil(promise);
    await promise;
    e.respondWith(new Response(`Redirected successfully!`));
});

The component throws with the following output:

stderr [0] :: Warning: Promise passed to FetchEvent#waitUntil was rejected with error. Pending tasks after that error might not run. Error details:
stderr [0] :: Didn't redirect to example.com
stderr [0] :: Stack:
stderr [0] ::   promise<@component.js:19:23
stderr [0] :: 

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions