Can't get a Worker to resolve even though a similar build from the one that works

Hey friends,

I have a worker that shows the contents of blogstatic-blog.bstatic.io when visiting blogstatic.io/blog.

However, with the same exact build blogstatic.io/blogging-success won’t show the contents of success.bstatic.io.

Here are the workers:

This one works fine
https://for-main-blogstatic-blog.v7958.workers.dev/ with blogstatic.io/blog* trigger

This one doesn’t
https://for-blogging-success.v7958.workers.dev/ with blogstatic.io/blogging-success* trigger

This is the code I am using on both workers, but with different paths, of course.

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
  const url = new URL(request.url)
  url.hostname = 'success.bstatic.io'
  url.pathname = url.pathname.replace(/^\/blog(\/)?/, '/')

  const response = await fetch(url)

  return new HTMLRewriter().on('[href]', {
    element(e) {
      let href = e.getAttribute('href')
      if (href.startsWith('/')) {
        href = `/blog${href}`
      } else if (href.startsWith('https://success.bstatic.io/')) {
        href = href.replace('https://success.bstatic.io', '/blog')
      }
      e.setAttribute('href', href)
    }
  }).on('[src]', {
    element(e) {
      let src = e.getAttribute('src')
      if (src.startsWith('/')) {
        src = `/blog${src}`
      } else if (src.startsWith('https://success.bstatic.io/')) {
        src = src.replace('https://success.bstatic.io', '/blog')
      }
      e.setAttribute('src', src)
    }
  }).transform(response)
}

Does anyone have any clue about why I can’t get it to work?

Hi @valsopi

Have a look at the Cloudflare Workers Routes documentation

Hi @anon9246926,

The routes were fine.

The issue was on my code:
src = "/blog${src}"

This should have been:
src = "/blogging-success${src}"

Same as the Route:
https://blogstatic.io/blogging-success*

And here’s the entire re-written code:

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
  const url = new URL(request.url)
  url.hostname = 'success.bstatic.io'
  url.pathname = url.pathname.replace(/^\/blogging-success(\/)?/, '/')

  const response = await fetch(url)

  return new HTMLRewriter().on('[href]', {
    element(e) {
      let href = e.getAttribute('href')
      if (href.startsWith('/')) {
        href = `/blogging-success${href}`
      } else if (href.startsWith('https://success.bstatic.io/')) {
        href = href.replace('https://success.bstatic.io', '/blogging-success')
      }
      e.setAttribute('href', href)
    }
  }).on('[src]', {
    element(e) {
      let src = e.getAttribute('src')
      if (src.startsWith('/')) {
        src = `/blogging-success${src}`
      } else if (src.startsWith('https://success.bstatic.io/')) {
        src = src.replace('https://success.bstatic.io', '/blogging-success')
      }
      e.setAttribute('src', src)
    }
  }).transform(response)
}

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.