You are here

class CssCollectionRenderer in HTTP/2 Server Push 8

Decorates the CSS collection renderer service, adds Server Push.

Hierarchy

Expanded class hierarchy of CssCollectionRenderer

See also

\Drupal\Core\Asset\CssCollectionRenderer

\Drupal\http2_server_push\Render\HtmlResponseAttachmentsProcessor

1 string reference to 'CssCollectionRenderer'
http2_server_push.services.yml in ./http2_server_push.services.yml
http2_server_push.services.yml
1 service uses CssCollectionRenderer
asset.css.collection_renderer.http2_server_push in ./http2_server_push.services.yml
\Drupal\http2_server_push\Asset\CssCollectionRenderer

File

src/Asset/CssCollectionRenderer.php, line 14

Namespace

Drupal\http2_server_push\Asset
View source
class CssCollectionRenderer implements AssetCollectionRendererInterface {
  use AssetHtmlTagRenderElementTrait;

  /**
   * The decorated CSS collection renderer.
   *
   * @var \Drupal\Core\Asset\AssetCollectionRendererInterface
   */
  protected $cssCollectionRenderer;

  /**
   * The request stack.
   *
   * @var \Symfony\Component\HttpFoundation\RequestStack
   */
  protected $requestStack;

  /**
   * Constructs a CssCollectionRenderer.
   *
   * @param \Drupal\Core\Asset\AssetCollectionRendererInterface $css_collection_renderer
   *   The decorated CSS collection renderer.
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   The request stack.
   */
  public function __construct(AssetCollectionRendererInterface $css_collection_renderer, RequestStack $request_stack) {
    $this->cssCollectionRenderer = $css_collection_renderer;
    $this->requestStack = $request_stack;
  }

  /**
   * {@inheritdoc}
   */
  public function render(array $css_assets) {
    $elements = $this->cssCollectionRenderer
      ->render($css_assets);
    $request = $this->requestStack
      ->getCurrentRequest();
    $link_headers = $request->attributes
      ->get('http2_server_push_link_headers', []);
    foreach ($elements as &$element) {
      if (!static::isLinkRelStylesheet($element)) {
        continue;
      }

      // Locally served CSS files that are sent to all browsers can be pushed.
      if (isset($element['#attributes']['href']) && static::hasRootRelativeUrl($element, 'href') && static::isUnconditional($element)) {
        $link_header_value = '<' . $element['#attributes']['href'] . '>; rel=preload; as=style';
        $link_headers[] = $link_header_value;

        // @todo When this is moved into Drupal core, consider allowing bubbling
        // of bubbleable metadata from the return value of
        // \Drupal\Core\Render\HtmlResponseAttachmentsProcessor::processAssetLibraries
        // so this line of code can work, which would mean we would no longer
        // need to use request attributes. (The problem is that a key assumption
        // is that rendering set of asset libraries to HTML that loads CSS/JS,
        // no further attachment bubbling happens. That's a fine assumption, but
        // this module is the first and sole exception.)

        /*
                $element['#attached']['http_header'][] = ['Link', $link_header_value];
        */
      }
    }
    $request->attributes
      ->set('http2_server_push_link_headers', $link_headers);
    return $elements;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AssetHtmlTagRenderElementTrait::hasRootRelativeUrl protected static function Whether the given attribute on the render element has a root-relative URL.
AssetHtmlTagRenderElementTrait::isLinkRelStylesheet protected static function Whether the render element is a <link rel=stylesheet>.
AssetHtmlTagRenderElementTrait::isScript protected static function Whether the render element is a <script>.
AssetHtmlTagRenderElementTrait::isUnconditional protected static function Whether the render element is unconditional.
CssCollectionRenderer::$cssCollectionRenderer protected property The decorated CSS collection renderer.
CssCollectionRenderer::$requestStack protected property The request stack.
CssCollectionRenderer::render public function Renders an asset collection. Overrides AssetCollectionRendererInterface::render
CssCollectionRenderer::__construct public function Constructs a CssCollectionRenderer.