DEV Community

Cover image for How we built a 3.4 KB ad script with 0.03 CLS using Cloudflare Workers
Salman Raja
Salman Raja

Posted on

How we built a 3.4 KB ad script with 0.03 CLS using Cloudflare Workers

If you run a developer blog, you know the absolute dread of adding a traditional display ad network. You spend months optimization-tuning your site to hit a perfect mobile Lighthouse score, only to watch a legacy tracking script pull down 100 KB of third-party bloat, thrash the DOM, destroy your Interaction to Next Paint (INP), and wreck your web vitals.

We got tired of this trade-off, so we built AsynchAds—a privacy-first sponsorship network tailored exclusively for tech blogs and documentation sites.
To prove it’s possible to monetize without ruining user experience, we open-sourced our operational metrics. Here is the technical breakdown of how we optimized our client-side footprint down to a microscopic 1-3 KB:

  1. Decoupling Compute from Origin via Cloudflare Workers

Instead of relying on heavy corporate analytics pipelines or database lookups on the client-side, we route everything through Cloudflare Workers.
• Ad requests are evaluated globally at the edge close to your reader.
• The Worker computes geographic targeting, checks campaign status, and renders payload structures instantaneously.
• Images are compressed dynamically into highly optimized WebP and PNG assets under 150 KB.

  1. Bulletproof Telemetry via navigator.sendBeacon

Traditional tracking scripts register click logs by spinning up continuous event loops that choke the browser's main thread. We completely bypassed this by utilizing navigator.sendBeacon. When a reader clicks a sponsored card, the click data is dispatched out-of-band as an asynchronous background request. The user experiences 0ms interaction lag, protecting your site’s INP.

  1. Viewport-Aware Impressions via IntersectionObserver

We don't believe in cheating advertisers by firing impression pixels the split-second a page mounts. Our vanilla script hooks into the native browser IntersectionObserver API with a strict 0.01 threshold. The payload tracking only triggers when a reader actually scrolls the placement into view.

  1. Safe Failures with 0 Cumulative Layout Shift (CLS)

If a user is browsing via an aggressive network-level firewall (like a Pi-hole) or loses connection entirely, our script catches the network error gracefully:

*.catch(() => {
container.style.display = "none";
});

Because the CSS containers match strict, predictable aspect ratios, the ad container collapses instantly and invisibly if a fetch fails. Your text content flows together perfectly—no broken image graphics, no awkward loading states, and no layout shifting.

Check out our raw site performance or audit our live placements directly on our Website.
We built this because developers deserve a fast web.

Top comments (1)

Collapse
 
salman_raja profile image
Salman Raja •

To test our core client-side tag (content.js), please refer to either of our active environments:
• Production Demo: asynchads.com/ad-demo
• Testing Blog: siliconspins.great-site.net