Skip to content

Commit

Permalink
Merge pull request #544 from ampproject/feature/fetchpriority-on-hero…
Browse files Browse the repository at this point in the history
…-images

Add `fetchpriority=high` on hero images
  • Loading branch information
westonruter authored Oct 24, 2023
2 parents b9e2df1 + 16b9f66 commit c79a0fe
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Html/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ interface Attribute
const EXTRA_SPACE = 'extra-space';
const FALLBACK = 'fallback';
const FETCH_ERROR = 'fetch-error';
const FETCHPRIORITY = 'fetchpriority';
const FILL = 'fill';
const FILL_OPACITY = 'fill-opacity';
const FILL_RULE = 'fill-rule';
Expand Down
26 changes: 26 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->appendChild($document->createAttribute(Attribute::DATA_HERO));
}
}

$heroImage = $this->detectImageWithAttribute($node, Attribute::DATA_HERO);
if ($heroImage) {
$heroImages[] = $heroImage;
Expand Down Expand Up @@ -566,6 +584,14 @@ private function generateImg(HeroImage $heroImage, Document $document)
$imgElement->setAttribute(Attribute::LOADING, $noscript_img->getAttribute(Attribute::LOADING));
}

// Preserve the original fetchpriority attribute from the noscript fallback img.
if ($noscript_img->hasAttribute(Attribute::FETCHPRIORITY)) {
$imgElement->setAttribute(
Attribute::FETCHPRIORITY,
$noscript_img->getAttribute(Attribute::FETCHPRIORITY)
);
}

// Remove any noscript>img when an amp-img is pre-rendered.
$noscript_img->parentNode->parentNode->removeChild($noscript_img->parentNode);
} elseif (! $this->isMarkedAsHeroImage($element)) {
Expand Down
24 changes: 24 additions & 0 deletions tests/Optimizer/Transformer/OptimizeHeroImagesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,30 @@ public function dataTransform()
. '</amp-story-player>'
),
],

'preserves fetchpriority attribute from noscript fallback img' => [
$input(
'<amp-img width="500" height="400" src="/img1.png"><noscript><img src="/img1.png" width="500" height="400" fetchpriority="high"></noscript></amp-img>'
),
$output(
'<amp-img data-hero width="500" height="400" src="/img1.png" i-amphtml-ssr><img class="i-amphtml-fill-content i-amphtml-replaced-content" decoding="async" fetchpriority="high" src="/img1.png"></amp-img>'
),
],

'automatically ssr img element with high fetchpriority attribute from noscript fallback img even when not a hero image' => [
$input(
'<amp-img width="500" height="400" src="/img1.png"></amp-img>'
. '<p>Hello World 1</p>'
. '<p>Hello World 2</p>'
. '<amp-img width="500" height="400" src="/img2.png"><noscript><img src="/img2.png" width="500" height="400" fetchpriority="high"></noscript></amp-img>'
),
$output(
'<amp-img width="500" height="400" src="/img1.png"></amp-img>'
. '<p>Hello World 1</p>'
. '<p>Hello World 2</p>'
. '<amp-img data-hero width="500" height="400" src="/img2.png" i-amphtml-ssr><img class="i-amphtml-fill-content i-amphtml-replaced-content" decoding="async" fetchpriority="high" src="/img2.png"></amp-img>'
),
],
];
}

Expand Down

0 comments on commit c79a0fe

Please sign in to comment.