class AddStaleHeaders in Fastly 8.3
Adds stale headers.
Hierarchy
- class \Drupal\fastly\EventSubscriber\AddStaleHeaders implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
Expanded class hierarchy of AddStaleHeaders
See also
https://docs.fastly.com/guides/purging/soft-purges
1 string reference to 'AddStaleHeaders'
1 service uses AddStaleHeaders
File
- src/
EventSubscriber/ AddStaleHeaders.php, line 16
Namespace
Drupal\fastly\EventSubscriberView source
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;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AddStaleHeaders:: |
protected | property | The config factory. | |
AddStaleHeaders:: |
protected | property | The Fastly logger channel. | |
AddStaleHeaders:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
AddStaleHeaders:: |
public | function | Adds Surrogate-Control header. | |
AddStaleHeaders:: |
public | function | Constructs a new CacheTagsHeaderLimitDetector object. |