info

Affiliate Disclosure: This article contains benchmarked tools that may compensate us. We only recommend hardware and software tested in our lab environments for maximum LCP and INP efficiency.

Configuring Cloudflare Workers for Custom Cache-Key Logic in WordPress

How to implement geolocation-based caching without sacrificing cache hit ratios using Cloudflare Workers.

calendar_today schedule — min read folder
format_list_bulleted

Table of Contents

Why Default Cloudflare Caching Falls Short

Cloudflare’s default cache key is based on URL path and query string. This means visitors requesting /shop/ receive the same cached response. But WordPress often generates different content based on cookies (WooCommerce cart state), device type, and geolocation. Cloudflare Workers allow you to intercept every request and programmatically modify the cache key.

Worker: WooCommerce Cart-Aware Caching

The most important customization: bypass cache when a visitor has items in their cart. While Cloudflare APO handles this for the standard woocommerce_items_in_cart cookie, custom setups can use Workers to explicitly bypass cache or serve cart-specific responses based on request headers and cookies.

Worker: Device-Type Cache Segmentation

If your theme uses wp_is_mobile() to serve different HTML, the same URL must be cached separately for mobile and desktop. A Worker can append the CF-Device-Type header to the cache key, ensuring mobile users receive the mobile cache and desktop users receive the desktop cache.

mark_email_read

Get Weekly Performance Benchmarks

Join 2,400+ WordPress engineers. Real lab data, no fluff.

No spam. Unsubscribe anytime.

engineering

About the Lab & Author

This benchmark and optimization guide was written by the Performance Engineering team at CoreVitalsLab. With over a decade of experience optimizing high-traffic WooCommerce stores and enterprise WordPress deployments, we focus exclusively on data-driven TTFB reduction, caching architecture, and passing Core Web Vitals.