You are here

AddStaleHeaders.php in Fastly 8.3

File

src/EventSubscriber/AddStaleHeaders.php
View source
<?php

namespace Drupal\fastly\EventSubscriber;

use Drupal\Core\Config\ConfigFactoryInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * Adds stale headers.
 *
 * @see https://docs.fastly.com/guides/purging/soft-purges
 */
class AddStaleHeaders implements EventSubscriberInterface {

  /**
   * The Fastly logger channel.
   *
   * @var \Psr\Log\LoggerInterface
   */
  protected $logger;

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $config;

  /**
   * Constructs a new CacheTagsHeaderLimitDetector object.
   *
   * @param \Psr\Log\LoggerInterface $logger
   *   The Fastly logger channel.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   */
  public function __construct(LoggerInterface $logger, ConfigFactoryInterface $config_factory) {
    $this->logger = $logger;
    $this->config = $config_factory;
  }

  /**
   * Adds Surrogate-Control header.
   *
   * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
   *   The event to process.
   */
  public function onRespond(FilterResponseEvent $event) {

    // Get the fastly settings from configuration.
    $config = $this->config
      ->get('fastly.settings');

    // Only modify the master request.
    if (!$event
      ->isMasterRequest()) {
      return;
    }

    // Get response.
    $response = $event
      ->getResponse();

    // Build the Surrogate-Control header.
    $cache_control_header = $response->headers
      ->get('Cache-Control');
    $surrogate_control_header = $cache_control_header;
    if ((bool) $config
      ->get('stale_while_revalidate')) {
      $surrogate_control_header = $surrogate_control_header . ', stale-while-revalidate=' . $config
        ->get('stale_while_revalidate_value');
    }
    if ((bool) $config
      ->get('stale_if_error')) {
      $surrogate_control_header .= ', stale-if-error=' . $config
        ->get('stale_if_error_value');
    }

    // Set the modified Cache-Control header.
    $response->headers
      ->set('Surrogate-Control', $surrogate_control_header);

    // Add something to indicate that this page was generated by
    // the Drupal generator instead of being a static HTML document.
    // We use it in VCL to set proper Cache-Control headers.
    $response->headers
      ->set('Fastly-Drupal-HTML', "YES");
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[KernelEvents::RESPONSE][] = [
      'onRespond',
    ];
    return $events;
  }

}

Classes

Namesort descending Description
AddStaleHeaders Adds stale headers.