---
title: "Stop Flickering Your Way to Bad Data: Run A/B Tests at the Edge"
description: Client-side A/B testing slows the page and skews the data it collects. Move experiments to an edge worker and ship variants as finished HTML.
author: LETSGROW Dev Team
date: 2026-10-10
category: Analytics
tags: ["A/B Testing", "Edge Computing", "Cloudflare Workers", "Experimentation", "Core Web Vitals"]
url: "https://letsgrow.dev/blog/edge-ab-testing-cloudflare-workers-no-flicker-clean-data"
---
Client-side A/B testing is a tax you pay on every page view. A third-party script loads, hides your page, flips the variant, and reveals it a few hundred milliseconds later. You pay in Core Web Vitals, in flicker, and in biased data from visitors who bounced before the test script ever ran. My position: for any test on a landing page that matters, run the experiment at the edge, before the HTML leaves the CDN.

## Why Client-Side Testing Quietly Skews Your Results

The standard testing snippet blocks rendering with an anti-flicker style, waits for assignment, then mutates the DOM. Three things go wrong.

First, performance. The anti-flicker timeout adds directly to Largest Contentful Paint, and slow pages convert worse, which contaminates the very metric you are testing. Second, blockers. Privacy extensions and strict browsers drop testing scripts, so a chunk of your traffic silently falls into the control with no assignment record. Third, flicker bias. Visitors on slow connections see the original for a moment and then a swap, and that experience differs by variant.

::stat-block
- 100-400 ms | Typical render delay added by anti-flicker snippets
- 0 | DOM mutations needed when the variant is chosen at the edge
- 1 | Network round trip, the one you already make for the HTML
::

## What Edge Experimentation Actually Looks Like

An edge worker (Cloudflare Workers, Vercel Edge Middleware, Netlify Edge Functions, Fastly Compute) runs on every request before the page is served. The flow is simple:

1. Read an assignment cookie. If none exists, hash a stable visitor ID with the experiment name and bucket the visitor deterministically.
2. Rewrite the request to the variant path, or transform the response with a streaming HTML rewriter.
3. Set the assignment cookie, and expose the experiment and variant to your analytics layer.

Here is the core of a bucketing function. No library required:

```js
async function bucket(visitorId, experiment, weights) {
  const data = new TextEncoder().encode(visitorId + ':' + experiment);
  const hash = new Uint8Array(await crypto.subtle.digest('SHA-256', data));
  const n = ((hash[0] << 8) | hash[1]) / 65535;
  let acc = 0;
  for (const [variant, w] of Object.entries(weights)) {
    acc += w;
    if (n <= acc) return variant;
  }
  return Object.keys(weights)[0];
}
```

Because the hash is deterministic, the same visitor always lands in the same variant, even if the cookie is cleared and the ID is restored from a first-party identifier. The variant arrives as finished HTML. Nothing flickers, because nothing changes after paint.

## Where Teams Get It Wrong

The edge solves delivery. It does not solve measurement, and most failures happen there.

**Cache fragmentation.** If every variant URL varies on a cookie, your cache hit rate collapses. Vary on the bucket only, and cache each variant as its own object. Two variants means two cache entries, not thousands.

**Assignment without exposure.** Log an exposure event when the variant renders, not when the visitor is bucketed. Bucketing a bot that never loads the page inflates your denominators. Filter known crawlers in the worker before you assign.

**Sample ratio mismatch.** If you configured 50/50 and the data shows 52/48 on 100,000 sessions, something is broken: caching, redirects, or bot traffic. Run a chi-square check on every experiment before reading any conversion result.

**Peeking.** Edge tooling makes launching tests so cheap that teams stop predefining sample size. Fix the minimum runtime and sample before launch, and do not call winners early.

::compare-table
| Factor | Client-side testing | Edge testing |
| Flicker | Common, masked by hiding the page | None, variant is in the HTML |
| LCP impact | Adds snippet and anti-flicker delay | Neutral, often faster |
| Ad blocker loss | Meaningful | Near zero, first-party |
| Setup effort | Low, visual editor | Moderate, needs an engineer |
| Best for | Copy and layout tweaks by marketers | Landing pages, pricing, high-traffic funnels |
::

## A Rollout You Can Ship This Month

Start with one page: your highest-traffic landing page or pricing page. Do not migrate your whole program.

::checklist
- Pick one page and one hypothesis with a primary metric defined in advance
- Deploy a worker that buckets by hashed visitor ID and sets a first-party cookie
- Cache each variant separately and confirm cache hit rate stays above 90 percent
- Push experiment and variant into your dataLayer on render, not on assignment
- Filter known bots before assignment
- Run a sample ratio mismatch check before reading results
- Set minimum runtime and sample size before launch and do not stop early
::

Keep the visual editor for marketers who need quick copy tests on low-stakes pages. Move the pages that carry revenue to the edge. The split is clean: reversible, low-traffic tweaks stay client-side, and anything where speed and clean data matter gets decided before the first byte.

The takeaway: an experiment that slows the page is measuring its own damage. Serve the variant in the HTML, log exposure on render, check the sample ratio, and your test results finally mean what the dashboard says they mean.
