Cloudflare Workers问题:预览环境可见变更但生产站点无效果
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 likewww.coverme.co.il. Since your target URL is the main domaincoverme.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.
- Exact match:
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

