Make redirect with additional headers

How can I make a redirect, but also modify the headers of that direct? The: Response.redirect(url, status) only accepts two parameters.

You can’t pass headers with redirect.

If it’s a local redirect you could use Cookies or Params, to pass data from this Request to the next Request.

What’s stopping you from returning a new response with status 302 and the Location header yourself?

Well, actually nothing really. I think that’s a great workaround. So I guess it defiently is possible to add headers using a redirect, just that you cannot use the “native” Response.redirect for it.

Would be cool if the native method had a third parameter for options though, like so Response.direct(url, status, options).

Thanks a lot

No problem, imo Response.redirect should be renamed, it’s most useful in client-side service workers.

https://developer.mozilla.org/en-US/docs/Web/API/Response/redirect

Note : This is mainly relevant to the ServiceWorker API. A controlling service worker could intercept a page’s request and redirect it as desired. This will actually lead to a real redirect if a service worker sends it upstream.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.