Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Response.redirect is not a function #39

Open
stelescuraul opened this issue Mar 3, 2020 · 2 comments
Open

Response.redirect is not a function #39

stelescuraul opened this issue Mar 3, 2020 · 2 comments

Comments

@stelescuraul
Copy link

I`d like to be able to test the redirect functionality locally (as dummy test at least), but the Response.redirect function is not defined.

Do you have any plans on supporting it ?

const handler = async function(request) {
  return Response.redirect('some_url', 200);
};

addEventListener('fetch', async function(event) {
  event.respondWith(handler(event.request));
});
@gja
Copy link
Owner

gja commented Mar 4, 2020 via email

@innovate-invent
Copy link
Contributor

innovate-invent commented Mar 19, 2020

node-fetch doesn't have it supported but isomorphic-fetch does.
I just copy pasted the shim from isomorphic-fetch into my project:

const redirectStatuses = [301, 302, 303, 307, 308];

if (!Response.redirect) {
  Response.redirect = function(url, status= 302) {
    if (redirectStatuses.indexOf(status) === -1) {
      throw new RangeError(`Invalid status code: ${status}`);
    }

    return new Response(null, { status: status, headers: { location: url } });
  }
}

I think an upstream PR would be more appropriate.

mrbbot added a commit to mrbbot/cloudflare-worker-local that referenced this issue Dec 16, 2020
Adds Web Streams API to worker context, and switches to the titelmedia fork of node-fetch which uses Web Streams instead of NodeJS ones. Also closes issue gja#39, as @titelmedia/node-fetch implements Response.redirect.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants