Oct 17, 2016

Enable and Use X-Drupal-Cache-Tags in HTTP headers

While converting from Drupal 7's caching to that of Drupal 8, for debugging purposes I wanted to enable the display of  X-Drupal-Cache-Tags in HTTP headers.

How to do this is documented in the article CacheableResponseInterface and is actually simple, but I had enough trouble with getting this to work that I'm providing a few of my own notes here.

In Drupal 8 core, there is the file /sites/default/default.services.yml  Copy this file to the same directory, creating a new file named services.yml (assuming you don't already have such a file).

For our purposes, in services.yml the only key you need is http.response.debug_cacheability_headers.

In my situation, I ended up with a services.yml that only contained the following two lines, where the value of the key is changed to true. I deleted all of the other keys.

    http.response.debug_cacheability_headers: true

After creating and/or editing services.yml be sure to do "a container rebuild, which is necessary when changing a container parameter". One way to accomplish this is via Clear all caches in the admin UI.

Finally, to see the display of  X-Drupal-Cache-Tags in HTTP headers, I use the Chrome browser on a Linux system. It happens to be version 39.0.

Navigate to: More tools > Developer tools > Network tab

After loading a page whose cache tags you are interested in, on the left side of the Developer tools panel, click on the url for the page. Then on the right, click on the Headers subtab and look under Response Headers for the X-Drupal-Cache-Tags property. You will see a number of cache tags that come with Drupal core in addition to your own custom ones.