You are here

class HtmlResponseSubscriber in CDN 8.3

Hierarchy

  • class \Drupal\cdn\EventSubscriber\HtmlResponseSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of HtmlResponseSubscriber

1 string reference to 'HtmlResponseSubscriber'
cdn.services.yml in ./cdn.services.yml
cdn.services.yml
1 service uses HtmlResponseSubscriber
cdn.html_response_subscriber in ./cdn.services.yml
Drupal\cdn\EventSubscriber\HtmlResponseSubscriber

File

src/EventSubscriber/HtmlResponseSubscriber.php, line 11

Namespace

Drupal\cdn\EventSubscriber
View source
class HtmlResponseSubscriber implements EventSubscriberInterface {

  /**
   * The CDN settings service.
   *
   * @var \Drupal\cdn\CdnSettings
   */
  protected $settings;

  /**
   * @param \Drupal\cdn\CdnSettings $cdn_settings
   *   The CDN settings service.
   */
  public function __construct(CdnSettings $cdn_settings) {
    $this->settings = $cdn_settings;
  }

  /**
   * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
   *   The event to process.
   */
  public function onRespond(FilterResponseEvent $event) {
    $response = $event
      ->getResponse();
    if (!$response instanceof HtmlResponse) {
      return;
    }
    if (!$this->settings
      ->isEnabled()) {
      return;
    }

    // Optimal, so first.
    $this
      ->addPreConnectLinkHeaders($response);

    // Fallback, so second.
    $this
      ->addDnsPrefetchLinkHeaders($response);
  }

  /**
   * Adds DNS prefetch link headers to the HTML response.
   *
   * @param \Drupal\Core\Render\HtmlResponse $response
   *   The HTML response to update.
   *
   * @see https://www.w3.org/TR/resource-hints/#dns-prefetch
   * @todo Remove when http://caniuse.com/link-rel-preconnect has support in all browsers or is equivalent with http://caniuse.com/#feat=link-rel-dns-prefetch
   */
  protected function addDnsPrefetchLinkHeaders(HtmlResponse $response) {
    $domains = $this->settings
      ->getDomains();
    if (count($domains)) {
      $response->headers
        ->set('x-dns-prefetch-control', 'on');
      foreach ($domains as $domain) {
        $response->headers
          ->set('Link', '<//' . $domain . '>; rel=dns-prefetch', FALSE);
      }
    }
  }

  /**
   * Adds preconnect link headers to the HTML response.
   *
   * @param \Drupal\Core\Render\HtmlResponse $response
   *   The HTML response to update.
   *
   * @see https://www.w3.org/TR/resource-hints/#preconnect
   */
  protected function addPreconnectLinkHeaders(HtmlResponse $response) {
    foreach ($this->settings
      ->getDomains() as $domain) {
      $response->headers
        ->set('Link', '<//' . $domain . '>; rel=preconnect; crossorigin', FALSE);
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {

    // This event subscriber wants to directly manipulate the Symfony response
    // object's headers. Therefore we must run after
    // \Drupal\Core\Render\HtmlResponseAttachmentsProcessor::processAttachments,
    // which would otherwise overwrite us. That is called by
    // \Drupal\Core\EventSubscriber\HtmlResponseSubscriber (priority 0), so
    // use a lower priority.
    $events[KernelEvents::RESPONSE][] = [
      'onRespond',
      -10,
    ];
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
HtmlResponseSubscriber::$settings protected property The CDN settings service.
HtmlResponseSubscriber::addDnsPrefetchLinkHeaders protected function Adds DNS prefetch link headers to the HTML response.
HtmlResponseSubscriber::addPreconnectLinkHeaders protected function Adds preconnect link headers to the HTML response.
HtmlResponseSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
HtmlResponseSubscriber::onRespond public function
HtmlResponseSubscriber::__construct public function