Cloudflare Docs
Edit this page on GitHub
Set theme to dark (⇧+D)

Customize Caching with Cloudflare Rules

Caching is a fundamental feature of the Cloudflare Content Delivery Network (CDN). As such, our global network automatically caches a lot of content around the world. When visitors request a cached resource, it is served from the data center closest to them and therefore, the content loads faster.

You can speed up your site’s performance even further thanks to the multiple caching settings available in Cache rules or Page Rules (deprecated).

​​ Before you begin

Cloudflare does not cache HTML resources automatically. This prevents us from unintentionally caching pages that often contain dynamic elements. For example, the content on certain HTML pages may change based on specific visitor characteristics, such as authentication, personalization, and shopping cart information.

However, you can configure HTML caching through specific Cloudflare Page Rules settings. The degree of HTML caching flexibility varies based on your domain plan as described in the best practice sections below.

When configuring caching settings with Page Rules, you are essentially manipulating certain options of the Caching app. The difference lies in that through Page Rules, you apply the caching settings at the URL level (not the entire site), after matching a specific pattern defined in a custom page rule. That way, you have fine-grained control over which specific resources to cache.

For background information on Cloudflare’s caching tools and options, consult:

​​ Cache static, anonymous HTML (all domains)

All domain plans can use the Cache Everything setting of Page Rules.

However, this option caches all HTML regardless of the presence of dynamic content. If you use this approach to cache pages that contain dynamic content, it is quite likely that visitors will get information that is not intended for them.

To add a Cache Everything page rule:

  1. Within an application, go to Rules > Page Rules.
  2. Click either Create Page Rule, or the wrench icon next to an existing page rule.
  3. Fill in the appropriate URL pattern.
  4. Click Add a Setting, then select the Cache Level setting name.
  5. Select the Cache Everything setting value.
  6. Save and deploy the rule.

In conclusion, this recommendation is only appropriate for HTML pages that are static and anonymous. To learn more, refer to How do I use Cache Everything with Cloudflare?

​​ Cache HTML selectively (Business and Enterprise domains)

Customers with Business and Enterprise domains have additional Page Rules settings that can be combined to selectively cache HTML content based on whether the page contains dynamic information.

Below, we provide links to articles with specific instructions on implementing this recommendation. However, we can summarize the process as follows:

  1. Create a new page rule for the desired URL pattern.

  2. Add the following three settings:

  • Cache Level: Cache Everything - to act as catch all for static, anonymous content
  • Bypass Cache on Cookie (available to Business and Enterprise domains only) - to bypass caching everything if the request has a matching cookie
  • Edge Cache TTL - to specify how long Cloudflare should keep the cached resource in our edge network before asking the origin for it again
  1. Save and deploy your new rule

To learn more about the technique described, consult:

Note that the Cloudflare Caching app allows you to purge the cache so that new requests are sent to the origin for re-fetching. To learn more, refer to How do I purge my cache?