addEventListener('fetch', event => {
var url = new URL(event.request.url);
if (url.pathname.startsWith('/shop/') || url.pathname === '/shop') {
handleBlog(event, url);
} else {
event.respondWith(fetch(event.request));
}
})
async function handleBlog(event, url) {
var originUrl = url.toString().replace(
'https://www.domain.com/shop',
'https://alternate-server.com/shop');
originUrl = originUrl.toString().replace(
'https://domain.com/shop',
'https://alternate-server.com/shop');
event.respondWith(fetch(originUrl, { cf: { cacheEverything: true } }));
}
I’m trying to map the /shop subdirectory to another server and it works, except once you land on any page - i.e. /shop/ or /shop/product1.html all of the internal links are still linking to https://alternate-server.com/shop vs. domain.com/shop
I was previously using a nginx server and it was setup like this and the links mapped appropriately -
location /shop/ {
proxy_pass https://alternate-server.com/shop/;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
I’ve been banging my head against the wall. Any help is very much appreciated!