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 folder

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.