0

我正在使用 Cloudflare 中的工作人员来重写缓存属性

const OLD_URL = 'cdn.MYDOMAIN.com';
const NEW_URL = 'cdn-uk.MYDOMAIN.com';
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'));

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

/**
 * Respond to the request
 * @param {Request} request
 */
async function handleRequest(request) {
  let response = await fetch(request); 
  return rewriter.transform(response)
}

问题是我仍然看到很多使用 Dr.Flare chrome 扩展的“非 cloudflare”文件。检查这些显示图像正在被注入到 Shopify 中的内联样式中。如何使用重写器来确保捕获所有图像?

4

0 回答 0