diff --git a/src/Optimizer/Transformer/OptimizeHeroImages.php b/src/Optimizer/Transformer/OptimizeHeroImages.php index dcfee7197..fdd576dcf 100644 --- a/src/Optimizer/Transformer/OptimizeHeroImages.php +++ b/src/Optimizer/Transformer/OptimizeHeroImages.php @@ -113,6 +113,13 @@ final class OptimizeHeroImages implements Transformer */ const NOSCRIPT_IMG_XPATH_QUERY = './noscript/img'; + /** + * XPath query to relatively fetch all noscript > img[fetchpriority="high"] elements. + * + * @var string + */ + const NOSCRIPT_IMG_FETCHPRIORITY_HIGH_XPATH_QUERY = './noscript/img[@fetchpriority="high"]'; + /** * Regular expression pattern to extract the URL from a CSS background-image property. * @@ -215,6 +222,17 @@ private function findHeroImages(Document $document, $maxHeroImageCount) $seenParagraphCount++; } + if ($node->tagName === Extension::IMG && ! $node->hasAttribute(Attribute::DATA_HERO)) { + $highFetchpriorityImage = $document->xpath->query( + self::NOSCRIPT_IMG_FETCHPRIORITY_HIGH_XPATH_QUERY, + $node + )->item(0); + + if ($highFetchpriorityImage instanceof Element) { + $node->setAttribute(Attribute::DATA_HERO, ''); + } + } + $heroImage = $this->detectImageWithAttribute($node, Attribute::DATA_HERO); if ($heroImage) { $heroImages[] = $heroImage;