Allow dynamic 302 redirects in edge function to "pass through"?

I’ve read the docs and a number of forum posts, but it’s still not clear how to allow a dynamic redirect to “pass through” my edge function.

I’m proxying a 3rd party CMS, and users of that system can create page redirects, resulting in a Location header and 302 response code. It works except for the fact that a status 200 is always returned to my edge function, so the URL still points to the “forbidden/offline” page even though the user sees the “correct” content (to which they were redirected).

So for instance, if a user visits somedomain.com/admin when they’re not logged in, they are redirected to somedomain.com (home page). What happens is that the home page does indeed appear, but the URL still points to somedomain.com/admin.

I have no control over the redirects created, so static directives and pattern matching won’t work. Instead, I need to detect a redirect from my edge function to update the URL. (At least I think that’s what I need to do, but maybe I’m overlooking something.)

Here are some snippets from my edge function…

// Make rewritten request to 3rd party service
const response = await fetch( new Request( newRequestUrl, request ) )

// Always returns 200 (redirect already handled)
console.log( 'Status: ', response.status )

I thought I could just detect the Location header and rewrite it, but it seems that’s not possible b/c the redirect has already happened by the time my edge function gets the response.

Any info or insights would be much appreciated.

Oi! I just noticed the redirect options for the fetch() method! I will give that a try and report back.

1 Like

Yep, that was it! I just needed to use the manual redirect option, rewrite the Location header, and then pass the response along with a 302 status code. :+1: