Redirect method in Response object is not exists, but the documentation says it exists

The documentation says that Response object have redirect method. But if you access the Response.redirect method, your worker will raise an exception.

This minimal code to reproduce the issue:

addEventListener("fetch", event => {
    event.respondWith(
        (() => {
            try {
                const response = new Response();
                response.redirect("https://mozilla.org/");
            } catch (err) {
                return new Response(err.stack || err);
            }
            return new Response("test");
        })(),
    );
});

if you deploy the worker above and perform GET request, you will error like the following:

TypeError: (intermediate value).redirect is not a function
    at worker.js:1:995
    at worker.js:1:1098

Iā€™m not sure if this is a bug or outdated documentation.

A static method (if that existed in Javascript :wink:)

1 Like

Good catch @bayualsyah! Filed https://github.com/cloudflare/workers-docs/issues/879 to fix it.

Try Response.redirect("https://mozilla.org/") instead.

@sandro @harris Ah I see. Thanks! :pray: