Worker is not able to rewrite urls in the response body

Hi,

I created a worker that works fine when I test it in sandbox mode but then it doesn’t work when I enable some routes to test it in my real site.
My worker should retrieve content from a different page and it’s not doing it. It means the routes I set are not working.

Screenshot 1 is how my worker is configured. (I tried without https:// also, It did not work)
28%20AM

Screenshot 2 is how the page looks when testing it in the sandbox mode (and what actually is the desired behavior)

Screenshot 3 is how it looks when I visit the page from the browser (it’s not fetching the one from the other server, as you can see, the css and js code is missing)

How do I handle this?

Try route/blog/*

Can you rewrite addeventlistner to trigger one main function? Better for debugging and readability.

I tried with that first, It did not work

Please Paste your code here (omit any authentication credentials). Your use case is usually very straight forward.

addEventListener(‘fetch’, event => {
var url = new URL(event.request.url);
var isBlog = url.pathname.match(/blog/);
if(url.href === ‘https://www.12345.com/blog/’) {
url.href = url.href.replace(‘https://www.12345.com/blog/’, ‘https://blog.12345.com’);
event.respondWith(fetch(url));
} else {
handleBlog(event, url);
}
})

/**

  • Fetch and log a request
  • @param {Request} request
    */
    async function handleBlog(event, url) {
    var allowedAry = [ ‘/assets/’, ‘/blog’];
    allowedAry.map((item) => {
    if(url.pathname.startsWith(item)){
    url.hostname = url.hostname.replace(‘www.12345.com’, ‘blog.12345.com’);
    event.respondWith(fetch(url));
    }
    })
    }