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.

Measured impact of replacing edge-injected beacon.min.js with Faro /metrics.js
CheckCloudflare Auto-InjectFaro First-Party /metrics.js
Lighthouse Mobile Score99 / 100100 / 100
Legacy JavaScript Audit~10.2 KiB flagged0 KiB (Pass)
RUM Ingest Origincloudflareinsights.comSame-origin /cdn-cgi/rum
POST /cdn-cgi/rum? StatusHTTP/2 204HTTP/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 and if (...) { ... } polyfill blocks safely while skipping quoted strings and escaped characters.
  • Same-origin telemetry: rewrites upstream cloudflareinsights.com/cdn-cgi/rum to /cdn-cgi/rum, removing a third-party connection while Cloudflare's edge still accepts POST /cdn-cgi/rum? with HTTP/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.
strip.mjs — Core Faro Polyfill Stripperjs
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.

Terminal + Document HTMLbash
# 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>