Caching HTML with Drupal

Overview

Customers in all Cloudflare plans can cache HTML files using Cloudflare Page Rules. However, only customers in the Business and Enterprise plans are able to bypass HTML caching whenever a cookie is sent with a Bypass Cache on Cookie setting using Cloudflare Page Rules.

You can cache HTML of inactive users and static content including likes, images, scripts, and stylesheets when using Drupal with Cloudflare by using the Cache Everything Page Rule with Bypass Cache on Cookie enabled.

The Bypass Cache on Cookie Page Rule option is only available on the Business and Enterprise plans.

Enable Browser Cache Expiration

To avoid browser cache collisions, you must first enable Bypass Cache on Cookie in the Cloudflare dashboard. To do this,

1. Log in to your Cloudflare account.

2. Click on the Caching app.

3. Scroll down to Browser Cache Expirationand choose Respect Existing Headers.

browser_cache_expiration_respect_existing_headers.png

Now, you can configure the Bypass Cache on Cookie setting using Cloudflare Page Rules


Set Bypass Cache on Cookie

When the Bypass Cache on Cookie page rule matches the criteria we set, Cloudflare will cache static images and other files, without caching HTML. To configure Bypass Cache on Cookie using Page Rules

1. Log in to your Cloudflare account.

2. Click on the Page Rules app.

3. Click the Create Page Rule button and enter your domain. In the example below, the domain is www.orangeclouded.com. 

4. Configure the Page Rules settings as follows:

  • use the * wildcard operator on the Drupal installation path to match the entire domain,
  • then, set the Cache Level to Cache Everything,
  • text, set Bypass Cache on Cookie rule for the Drupal variables. This rule will override the Cache Everything rule when a user is logged into Drupal and
  • finally, set the Edge Cache TTL setting to determine how long a Cloudflare cache server should store cached files.

If you use additional cookies in your Drupal site you must ensure these are in the Regex statement: SESS.*|phpsessid=.*

page_rules_caching_static_HTML_with_drupal.png

Now, when Cloudflare is serving cached files, a CF-Cache-Status: HIT header will be sent back to the browser.


Related resources

Not finding what you need?

95% of questions can be answered using the search tool. This is the quickest way to get a response.

Powered by Zendesk