You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Cloudflare Workers问题:预览环境可见变更但生产站点无效果

Troubleshooting Your Cloudflare Workers HTML Injection Issue

Hey there, let's break down why your Worker works in preview but not on your production site, and fix it step by step:

1. Fix Your Cloudflare Route Configurations

Your current routes have critical formatting issues that prevent them from matching your production URL correctly:

  • Remove routes with https://: Cloudflare routes don't support including the protocol prefix — delete the third route entirely.
  • Drop subdomain-targeted routes: The second and fourth routes use *.coverme.co.il, which only matches subdomains like www.coverme.co.il. Since your target URL is the main domain coverme.co.il, these routes won't trigger for your desired page.
  • Use valid main-domain routes: For your specific product page, use either:
    • Exact match: coverme.co.il/product/נר-בינוני-tuberosejasmine/ (matches only the exact URL with trailing slash)
    • Flexible match: coverme.co.il/product/נר-בינוני-tuberosejasmine* (matches the page with/without trailing slash, plus any query parameters)
      Cloudflare matches routes against decoded URI paths, so using the actual Hebrew characters is more reliable than URL-encoded strings here.

2. Update the Content-Type Check in Your Worker

In your handleRequest function, the line if (contentType.startsWith("text/html")) might skip the HTML transformation in production. Many servers return text/html; charset=UTF-8 (with a charset suffix), which startsWith won't detect.

Replace that check with:

if (contentType?.includes("text/html")) {
  return rewriter.transform(res)
} else {
  return res
}

The ?. operator handles cases where contentType is null, and includes will match any Content-Type containing text/html, regardless of suffixes.

3. Clear Cloudflare Cache

Production sites often serve cached responses that haven't been processed by your Worker. To resolve this:

  • Head to your Cloudflare dashboard, go to Cache > Configuration
  • Use the Purge Cache tool to clear the cache for your specific product URL, or purge everything if needed
  • Alternatively, force-refresh your browser (Ctrl+F5 on Windows/Linux, Cmd+Shift+R on Mac) to bypass local cache.

4. Verify Worker-Route Association

Double-check that your Worker is linked to the corrected routes:

  • Go to your Worker's page in the Cloudflare dashboard
  • Under Triggers, confirm your fixed routes are listed and active.

After applying these changes, your HTML injection should work on the production site just like it does in the preview.

内容的提问来源于stack exchange,提问作者tarkil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 09:27:28