These days, Cloudflare is putting a lot of effort into revamping its cache system. The other day, I wrote about the new Cloudflare Cache Reserve system that had infinite retention using R2 storage. Now Cloudflare is splitting its well-known “Page Rules” system into different streams, with one stream dedicated solely to caching. Here’s how it works.
Using Cache Rules Instead of Page Cache
In your Cloudflare settings dashboard, go to “Caching -> Cache Rules,” as shown here:
As you can see, unlike Page Rules, which only allowed you to create rules based on URL patterns, Cache Rules let you filter out a lot more. For example, you can set separate caching rules for certain file formats, filter by the user agent, the referrer, and more. You’re not restricted to using only the options from the dropdown box – you can use any of the expressions allowed in the Cloudflare Fields Reference through the expression editor.
For example, the following filter applies to all PNG images:
any(http.request.headers["content-type"][*] == "image/png")
Of course, you could also filter by the “.png” filename extension. The point of the above example is to demonstrate the range of filters you can use with the Cloudflare Cache Rules. Once all the Page Rules are split into separate streams, I foresee they will vanish entirely. And why not?
Matching with “AND”/”OR” Rules
In addition to having greater control over which URLs get filtered, you can mix and match different conditions using “AND”/”OR” statements. So you can create detailed exceptions for caching. This opens up new avenues for caching. For example, you can create a cache rule for a URL and create an exception if it contains a particular query string. You can even mix this with Page Rule rewrites so that all links you click on a URL containing a certain query string will rewrite to use the same query string.
This kind of fine-grained caching is very cool!
Increased Limits for Free and Pro Tiers
The old Page Rules limit for free accounts was just three rules. With the new Cache Rules, that limit is ten for the free tier and 25 for the Pro tier. Here’s a screenshot of a free domain on Cloudflare with the new quota:
As you can see, this is a massive upgrade from what we had previously. Considering that we’ll get expanded limits for each breakdown, this represents a new level of control over the various aspects of traffic filtering, even for free users.
No Need for “Stale-While-Revalidate” Code
Earlier, I’d written about how to ensure that we serve stale content to users while refreshing content from the origin. This allows Cloudflare to delay serving fresh content just one more time while refreshing its own cache. It ensures that new content will be served as fast as old content.
With Cloudflare Cache rules, we no longer need to worry about fiddling with .htacess files. Instead, you can specify the stale-while-revalidate condition within the Cache Rules interface like this:
This is much easier than using a .htaccess directive that risks breaking your site. You can see from the screenshot that you can also use a bunch of other directives, such as specifying the EDGE TTL or even configuring the cache keys used to determine whether or not Cloudflare should serve separate cached content for device types and more.
Complete Caching Control
Cloudflare already had an impressive set of caching filters. But with the latest Cache Control feature it takes caching to the next level. Combined with Cache Reserve, it’s almost like having a secondary hosting provider at a fraction of the cost!
I’m a NameHero team member, and an expert on WordPress and web hosting. I’ve been in this industry since 2008. I’ve also developed apps on Android and have written extensive tutorials on managing Linux servers. You can contact me on my website WP-Tweaks.com!