Skip to content

Commit

Permalink
Update OptimizeHeroImages::generateImg() to auto SSR high fetchprio…
Browse files Browse the repository at this point in the history
…rity img
  • Loading branch information
thelovekesh committed Oct 24, 2023
1 parent 3852f92 commit 997f03b
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/Optimizer/Transformer/OptimizeHeroImages.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 997f03b

Please sign in to comment.