I have a simple reverse proxy - it’s working great apart from at certain times (and this is by design) the origin will return a redirect. The redirected content is being served on the proxied url (this is not desirable).
const DOMAIN = 'www.public.com';
const PROXYPATH = 'docs';
const ORIGIN = 'www.private.com';
addEventListener('fetch', event => {
var url = new URL(event.request.url);
if (url.pathname.startsWith('/' + PROXYPATH + '/') || url.pathname === '/' + PROXYPATH) {
handleRequest(event, url);
} else {
event.respondWith(fetch(event.request));
}
})
async function handleRequest(event, url) {
// Change URL from public URL to use the origin URL
var originUrl = url.toString().replace(
'https://' + DOMAIN + '/' + PROXYPATH,
'https://' + ORIGIN
);
event.passThroughOnException();
event.respondWith(fetch(originUrl));
}
So lets say ORIGIN www.private .com/docs is responding with a 302 to www. anothersite .com, the www.anothersite .com body would be there with www.public .com/docs in the browser url.
What I would like to happen is when the origin is responding with a redirect then the redirect happens in the users browser, i.e. it goes to www.anothersite .com.
(spaces added to get around link limit)