Redirects with edge functions

Is there a simple example of doing a redirect with edge functions?

I had assumed I would be able to use Response.redirect() from the Web API:

export default () => new Response.redirect("", 302)

This doesn’t work. Instead I am having to setup the headers and status of the returned response to make a redirect, which I’m guessing I shouldn’t need to do:

export default () => {
  const headers = {
    Location: ''
  const init = {
    status: 302,
  return new Response("do it!", init)

Am I missing a cleaner way?

Hey @sanscheese

Looking at the Web API documentation for Edge Functions, redirect is not supported (currently at least), so you need to use the Location header method.

1 Like

You can use the Response.redirect() method. It is a method, not a class, so you have to skip the new prefix.

See my gist for an example

1 Like

Is is possible to do rewrite/proxy? I want to implement A/B testing between netlify sites based on cookie value sent by browser.

Thank you! You are right, and I was sure I had it working like this the other day :smile:

Turns out I tried to set a cookie above the redirect which was resulting in an error:
Error: TypeError: Headers are immutable.

Removing that means this is enough for minimal redirect example:

export default () => Response.redirect("", 302)

(I should have tested my minimal example before posting!)

rewrite/proxy are possible
rewrite example: Rewrite with Edge Functions | Edge Functions on Netlify
Proxy example: Proxy requests to another source | Edge Functions on Netlify

1 Like

Ah yes, of course, should have spotted the errant new.

Best my understanding, no headers, etc, are settable with Response.redirect(), so use case would dictate method used.