Hey Matteo,
I really appreciate you drawing this up for me… I couldn’t get it to work yet though but I’m hopeful. For the sake of sharing some real URL’s with you I’ve set up an extra domain I have on Cloudflare and purchased a worker for it. I’ll give you the breakdown of where I’m at:
My domain is www.bdiq.com/directory and I am attempting to proxy/render https://agencies.partnerpage.io/directory/ which is my product page. Below I have modified that code you shared with me… see the example here:
addEventListener(“fetch”, event => {
let requestURI = new URL(event.request.url);
if (requestURI.hostname == “www.bdiq.com” && requestURI.path.startsWith(“/directory”)) {
event.respondWith(rewriteURL(event.request));
}
})
async function rewriteURL(request) {
var URI = new URL(request.url);
URI.hostname = “agencies.partnerpage.io”;
return fetch(URI, request);
}
I created a route of “www.bdiq.com/directory” andmy hope is that I can have www.bdiq.com/directory and it will display agencies.partnerpage.io/directory but remain on my bdiq.com URL - the editor was telling me there was some issue with the JS: Uncaught (in response) TypeError: Cannot read property ‘startsWith’ of undefined
I remembered your comment about how I can remove part of the code and it was more to make the code robust and after reviewing your comment I changed the code to be
addEventListener(“fetch”, event => {
let requestURI = new URL(event.request.url);
event.respondWith(rewriteURL(event.request));
})
async function rewriteURL(request) {
var URI = new URL(request.url);
URI.hostname = “agencies.partnerpage.io”;
return fetch(URI, request);
}
At this point the URL is redirecting to the right place, but its not maintaining the bdiq.com domain… perhaps I am missing something more.
Let me know if you have any additional idea for me on this
Thank you!
Mario T