Rewrinting HTMl (HTMLRewriter) of Gatsby App by Workers doesn't work

Hi there,

I was trying to rewrite the HTML of the Gatsby app by Cloudflare Workers but it didn’t work.

Gatsby Web App URL:

Workers code:

const OLD_URL = "images.ctfassets.net"
const NEW_URL = "images-rewrite.ctfassets.net"

async function handleRequest(req) {
  const res = await fetch(req)
  return rewriter.transform(res)
}

class AttributeRewriter {
  constructor(attributeName) {
    this.attributeName = attributeName
  }
  element(element) {
    const attribute = element.getAttribute(this.attributeName)
    if (attribute) {
      element.setAttribute(
        this.attributeName,
        attribute.replace(OLD_URL, NEW_URL),
      )
    }
  }
}

const rewriter = new HTMLRewriter()
  .on("a", new AttributeRewriter("href"))
  .on("img", new AttributeRewriter("src"))
  .on("source", new AttributeRewriter("data-srcset"))
  .on("source", new AttributeRewriter("srcset"))

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

I’d appreciate if anyone could give me some advice.

Thanks,
Ryo