You are here

public function CacheableResponseSubscriber::onRespond in Pantheon Advanced Page Cache 8

Same name in this branch
  1. 8 src/EventSubscriber/CacheableResponseSubscriber.php \Drupal\pantheon_advanced_page_cache\EventSubscriber\CacheableResponseSubscriber::onRespond()
  2. 8 tests/modules/pantheon_advanced_page_cache_test/src/EventSubscriber/CacheableResponseSubscriber.php \Drupal\pantheon_advanced_page_cache_test\EventSubscriber\CacheableResponseSubscriber::onRespond()

Adds Surrogate-Key header to cacheable master responses.

Parameters

\Symfony\Component\HttpKernel\Event\FilterResponseEvent $event: The event to process.

File

tests/modules/pantheon_advanced_page_cache_test/src/EventSubscriber/CacheableResponseSubscriber.php, line 21

Class

CacheableResponseSubscriber
Adds Surrogate-Key header to cacheable master responses.

Namespace

Drupal\pantheon_advanced_page_cache_test\EventSubscriber

Code

public function onRespond(FilterResponseEvent $event) {
  if (!$event
    ->isMasterRequest()) {
    return;
  }
  $response = $event
    ->getResponse();
  if ($response instanceof CacheableResponseInterface) {
    $tags = $response
      ->getCacheableMetadata()
      ->getCacheTags();

    // This is a contrived example of how custom code can be used
    // to limit a giant list of tags.
    // In this case, automated Behat tests generate nodes
    // tagged in 100s of taxonomy terms each. Then when
    // those nodes are rendered on a view like frontpage
    // they result in too many total surrogate-keys being set.
    if (in_array("config:views.view.frontpage", $tags)) {
      $new_tags = [];
      foreach ($tags as $tag) {
        if (strpos($tag, "taxonomy_term:") === FALSE) {
          $new_tags[] = $tag;
        }
      }
      $response
        ->getCacheableMetadata()
        ->setCacheTags($new_tags);
    }
  }
}