There is a uniquely frustrating moment in web performance engineering: your SSR HTML is gzipped, critical CSS is inlined, Total Blocking Time is 0 ms, Cumulative Layout Shift is 0.000, Largest Contentful Paint finishes under a second—and Google PageSpeed Insights still caps Mobile Performance at 99/100.
Expanding the diagnostics reveals one lingering warning: Avoid serving legacy JavaScript to modern browsers. The offending script is not yours. It is https://static.cloudflareinsights.com/beacon.min.js, automatically injected at the edge by Cloudflare Web Analytics. Bundled inside sit guarded ES polyfills for Array.prototype.at, Array.prototype.findLast, Array.prototype.includes, and Object.hasOwn—roughly 10.2 KiB of legacy code that Lighthouse penalizes on every mobile run.
| Check | Cloudflare Auto-Inject | Faro First-Party /metrics.js |
|---|---|---|
| Lighthouse Mobile Score | 99 / 100 | 100 / 100 |
| Legacy JavaScript Audit | ~10.2 KiB flagged | 0 KiB (Pass) |
| RUM Ingest Origin | cloudflareinsights.com | Same-origin /cdn-cgi/rum |
| POST /cdn-cgi/rum? Status | HTTP/2 204 | HTTP/2 204 |
Evolution: From Storefront Build Script to Any-Stack Tooling
We first solved this inside our Shopify Hydrogen storefronts with a build script that fetched beacon.min.js, stripped the guarded polyfill definitions, rewrote the ingest URL from https://cloudflareinsights.com/cdn-cgi/rum to same-origin /cdn-cgi/rum, and emitted a first-party public/metrics.js file.
Because every Cloudflare-hosted site—Astro, Next.js, React Router, Rails, or static HTML—hits the exact same edge injection, we generalized the engine into Faro with zero runtime dependencies and balanced delimiter scanning.
- Balanced delimiter stripping: matches guarded
Object.defineProperty(...)calls andif (...) { ... }polyfill blocks safely while skipping quoted strings and escaped characters. - Same-origin telemetry: rewrites upstream
cloudflareinsights.com/cdn-cgi/rumto/cdn-cgi/rum, removing a third-party connection while Cloudflare's edge still acceptsPOST /cdn-cgi/rum?withHTTP/2 204. - Two deployment modes: deploy a 25-line Zero-Build Cloudflare Worker on
/metrics.js, or run the zero-dependency CLI during your normal build.
import { stripCloudflareBeacon } from "faro/strip";
const { code, strippedCount, savedBytes, remainingPolyfills } =
stripCloudflareBeacon(rawBeaconJs, {
endpoint: "/cdn-cgi/rum",
});
// Output:
// remainingPolyfills => []
// endpoint => "/cdn-cgi/rum"Zero-Build Worker vs. Build-Time CLI
If you prefer zero changes to your application build pipeline, Faro ships a Cloudflare Worker that intercepts /metrics.js, fetches upstream beacon.min.js with a 24-hour edge cache, strips every legacy polyfill in memory, and serves the clean bundle with Cache-Control: public, max-age=86400.
# Build-time CLI mode (Node, Bun, or Deno)
npx github:roier/faro --out public/metrics.js
# First-party deferred tag in production HTML:
# <script defer src="/metrics.js"
# data-cf-beacon='{"token":"YOUR_SITE_TOKEN","send":{"to":"/cdn-cgi/rum"}}'>
# </script>