You are here

public function AddStaleHeaders::onRespond in Fastly 8.3

Adds Surrogate-Control header.

Parameters

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

File

src/EventSubscriber/AddStaleHeaders.php, line 51

Class

AddStaleHeaders
Adds stale headers.

Namespace

Drupal\fastly\EventSubscriber

Code

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");
}