The core problem
Server-side A/B testing creates a caching conflict with CDNs like Cloudflare. The issue is timing: Cloudflare chooses whether to serve from cache before your origin decides the variant. When your PHP tool assigns users to variant A or B and sets a cookie on the response, Cloudflare cannot determine which cached version to serve on the next request.
This results in either disabled HTML caching (due to Set-Cookie headers) or incorrect variant assignment when users encounter the wrong cached version.
The solution: custom cache keys
The recommended approach leverages Cloudflare’s Custom Cache Key feature. Your server-side testing tool remains responsible for variant assignment and cookie creation, while Cloudflare splits its cache based on the experiment cookie value alone.
Implementation steps
- Modify your PHP (or whatever) logic to set the
ab_variantcookie only once on first visit, never repeatedly. - Ensure HTML remains cacheable with appropriate headers like
Cache-Control: public, max-age=0, s-maxage=300. - Configure a Cloudflare Cache Rule that includes
Cookie: ab_variantin the custom cache key. - Set an appropriate Edge Cache TTL for your experiment refresh cadence.
Key considerations
The first page view will likely miss cache since no cookie exists yet — this is expected. Subsequent requests with the cookie will hit the appropriate cached variant.
Critical safeguards include:
- Prevent repeated
Set-Cookieheaders on every request. - Avoid bot contamination through deterministic assignment for known crawlers.
- Keep cache keys minimal to maintain a high hit rate. Every extra dimension in the key fragments the cache.
This approach preserves server-side testing integrity while restoring CDN caching performance — the best of both worlds. Caching is just one of the technical complexities that make CRO harder than it looks — but it’s among the most fixable.
Conversion Workers is an A/B testing platform built natively on Cloudflare Workers that handles custom cache key configuration out of the box. If you are building edge experiments on Cloudflare and want the caching, variant assignment and analytics attribution handled for you, see how it works.