diff --git a/README.md b/README.md index c2df80f..1ce74e3 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,16 @@ A direct way of inspecting headers is with `curl -I`. This command will make a r `curl -IH "Pantheon-Debug:1" https://dev-cache-tags-demo.pantheonsite.io/ | grep -i Surrogate-Key-Raw` +## Changing Listing Tags + +Prior to the 1.2 release, this module would change the cache tags used on default listings. +This changing of was done to make cache hits more likely but resulted in [confusing cache clearing behavior](https://www.drupal.org/project/pantheon_advanced_page_cache/issues/2944229). +Sites that installed this module prior to 1.2 should uninstall and reinstall or run this command to update their settings. + +``` +terminus drush [MACHINE-NAME-OF-SITE].[ENV-NAME] -- config:set pantheon_advanced_page_cache.settings --input-format=yaml "override_list_tags" "false" +``` + ## Limit on header size Pantheon's nginx configuration limits total header size to 32k. diff --git a/config/install/pantheon_advanced_page_cache.settings.yml b/config/install/pantheon_advanced_page_cache.settings.yml new file mode 100644 index 0000000..68e445b --- /dev/null +++ b/config/install/pantheon_advanced_page_cache.settings.yml @@ -0,0 +1 @@ +override_list_tags: false \ No newline at end of file diff --git a/pantheon_advanced_page_cache.install b/pantheon_advanced_page_cache.install new file mode 100644 index 0000000..eb7f642 --- /dev/null +++ b/pantheon_advanced_page_cache.install @@ -0,0 +1,13 @@ +getEditable('pantheon_advanced_page_cache.settings')->set('override_list_tags', TRUE)->save(); +} diff --git a/pantheon_advanced_page_cache.services.yml b/pantheon_advanced_page_cache.services.yml index 6b0326c..266c239 100644 --- a/pantheon_advanced_page_cache.services.yml +++ b/pantheon_advanced_page_cache.services.yml @@ -8,6 +8,6 @@ services: - { name: cache_tags_invalidator } pantheon_advanced_page_cache.cacheable_response_subscriber: class: Drupal\pantheon_advanced_page_cache\EventSubscriber\CacheableResponseSubscriber - arguments: ['@logger.channel.pantheon_advanced_page_cache'] + arguments: ['@logger.channel.pantheon_advanced_page_cache', '@config.factory'] tags: - { name: event_subscriber } diff --git a/src/EventSubscriber/CacheableResponseSubscriber.php b/src/EventSubscriber/CacheableResponseSubscriber.php index 905b535..b9a72a9 100644 --- a/src/EventSubscriber/CacheableResponseSubscriber.php +++ b/src/EventSubscriber/CacheableResponseSubscriber.php @@ -8,6 +8,7 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Psr\Log\LoggerInterface; use Drupal\Core\Logger\RfcLogLevel; +use Drupal\Core\Config\ConfigFactoryInterface; /** * Adds Surrogate-Key header to cacheable master responses. @@ -21,14 +22,44 @@ class CacheableResponseSubscriber implements EventSubscriberInterface { */ protected $logger; + /** + * Configuration Factory. + * + * @var \Drupal\Core\Config\ConfigFactoryInterface + */ + protected $configFactory; + /** * Constructs a new DefaultExceptionHtmlSubscriber. * * @param \Psr\Log\LoggerInterface $logger * The logger service. + * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory + * Configuration for this module. */ - public function __construct(LoggerInterface $logger) { + public function __construct(LoggerInterface $logger, ConfigFactoryInterface $config_factory = NULL) { + if (!$config_factory instanceof ConfigFactoryInterface) { + @trigger_error('Not passing the config factory service as the second parameter to ' . __METHOD__ . ' is deprecated in pantheon_advanced_page_cache:8.x-1.2 and will throw a type error in pantheon_advanced_page_cache:8.x-2.0. Pass an instance of \\Drupal\\Core\\Config\\ConfigFactoryInterface. See https://www.drupal.org/node/2944229', E_USER_DEPRECATED); + $config_factory = \Drupal::service('config.factory'); + } $this->logger = $logger; + $this->configFactory = $config_factory; + } + + /** + * Returns whether entity_list tags should be overridden. + * + * Overriding these tags was the initial behavior of the 1.0 version of this + * module. That is no longer recommended. + */ + public function getOverrideListTagsSetting() { + $config = $this->configFactory->get('pantheon_advanced_page_cache.settings'); + // Only return FALSE if this config value is really set to false. + // A null value should return TRUE for backwards compatibility. + if ($config->get('override_list_tags') === FALSE) { + return FALSE; + } + return TRUE; } /** @@ -49,8 +80,10 @@ public function onRespond(FilterResponseEvent $event) { // Rename all _list cache tags to _emit_list to avoid clearing list cache // tags by default. - foreach ($tags as $key => $tag) { - $tags[$key] = str_replace('_list', '_emit_list', $tag); + if ($this->getOverrideListTagsSetting()) { + foreach ($tags as $key => $tag) { + $tags[$key] = str_replace('_list', '_emit_list', $tag); + } } $tags_string = implode(' ', $tags); diff --git a/tests/behat/features/override_list_tag.feature b/tests/behat/features/override_list_tag.feature new file mode 100644 index 0000000..a4ecbd1 --- /dev/null +++ b/tests/behat/features/override_list_tag.feature @@ -0,0 +1,21 @@ +Feature: Listing tags +In order to control caching of lists +As an administrator +I want to toggle the setting for overriding cache tags + + @api + Scenario: Core behavior + Given there are some "article" nodes + And "/" is caching + When a generate a "article" node + Then "/" has been purged + And "/" is caching + + @api @current + Scenario: Old override + Given there are some "article" nodes + When I run drush "config:set pantheon_advanced_page_cache.settings --input-format=yaml override_list_tags true" + And "/" is caching + When a generate a "article" node + And "/" has not been purged +