Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

How can we know if a page in Drupal 8 has changed?

Parent Feed: 

This was a question I got from a client. So I set to work on finding a solution to alert the team who needed to know when a page has changed.

TL;DR: CacheTags.

Drupal has an awesome caching layer. It makes use of cache tags, these tags can be invalidated when something changes. For example if this is a view called “frontpage”, it would output the cache tag “view:frontpage”. This view lists a bunch of nodes, each with their own cache tag, “node:1”, “node:2” etc. When node 2 is edited, it’s cache tag and the frontpage view cache tag would be invalidated.

Taking this into account if we want to know what pages have changed, we need to know what cache tags the page has, then what cache tags have been invalidated.

The client already has a crawler to check their sites, this can look at the X-Drupal-Cache-Tags header and capture the cache tags the page has. So all we need now is a way of telling the crawler what cache tags have been invalidated.

Welcome to the CacheTag Notify module. This has a simple settings page where an endpoint URL can be added. Then every time a cache tag is invalidated it gets POSTed to the endpoint as a JSON string. The crawler will then need to to lookup which pages are using the invalidated cache tags, then it knows which have changed.

The CacheTag Notify module works by adding CacheTagsInvalidator service. The invalidateTags method is passed an array of invalited tags, this is then POSTed to the endpoint url using Guzzle. Overall a very, very simple, but effective solution.

Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web