Skip to content

Commit c79a0fe

Browse files
authored
Merge pull request #544 from ampproject/feature/fetchpriority-on-hero-images
Add `fetchpriority=high` on hero images
2 parents b9e2df1 + 16b9f66 commit c79a0fe

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

src/Html/Attribute.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ interface Attribute
344344
const EXTRA_SPACE = 'extra-space';
345345
const FALLBACK = 'fallback';
346346
const FETCH_ERROR = 'fetch-error';
347+
const FETCHPRIORITY = 'fetchpriority';
347348
const FILL = 'fill';
348349
const FILL_OPACITY = 'fill-opacity';
349350
const FILL_RULE = 'fill-rule';

src/Optimizer/Transformer/OptimizeHeroImages.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,13 @@ final class OptimizeHeroImages implements Transformer
113113
*/
114114
const NOSCRIPT_IMG_XPATH_QUERY = './noscript/img';
115115

116+
/**
117+
* XPath query to relatively fetch all noscript > img[fetchpriority="high"] elements.
118+
*
119+
* @var string
120+
*/
121+
const NOSCRIPT_IMG_FETCHPRIORITY_HIGH_XPATH_QUERY = './noscript/img[@fetchpriority="high"]';
122+
116123
/**
117124
* Regular expression pattern to extract the URL from a CSS background-image property.
118125
*
@@ -215,6 +222,17 @@ private function findHeroImages(Document $document, $maxHeroImageCount)
215222
$seenParagraphCount++;
216223
}
217224

225+
if ($node->tagName === Extension::IMG && ! $node->hasAttribute(Attribute::DATA_HERO)) {
226+
$highFetchpriorityImage = $document->xpath->query(
227+
self::NOSCRIPT_IMG_FETCHPRIORITY_HIGH_XPATH_QUERY,
228+
$node
229+
)->item(0);
230+
231+
if ($highFetchpriorityImage instanceof Element) {
232+
$node->appendChild($document->createAttribute(Attribute::DATA_HERO));
233+
}
234+
}
235+
218236
$heroImage = $this->detectImageWithAttribute($node, Attribute::DATA_HERO);
219237
if ($heroImage) {
220238
$heroImages[] = $heroImage;
@@ -566,6 +584,14 @@ private function generateImg(HeroImage $heroImage, Document $document)
566584
$imgElement->setAttribute(Attribute::LOADING, $noscript_img->getAttribute(Attribute::LOADING));
567585
}
568586

587+
// Preserve the original fetchpriority attribute from the noscript fallback img.
588+
if ($noscript_img->hasAttribute(Attribute::FETCHPRIORITY)) {
589+
$imgElement->setAttribute(
590+
Attribute::FETCHPRIORITY,
591+
$noscript_img->getAttribute(Attribute::FETCHPRIORITY)
592+
);
593+
}
594+
569595
// Remove any noscript>img when an amp-img is pre-rendered.
570596
$noscript_img->parentNode->parentNode->removeChild($noscript_img->parentNode);
571597
} elseif (! $this->isMarkedAsHeroImage($element)) {

tests/Optimizer/Transformer/OptimizeHeroImagesTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,30 @@ public function dataTransform()
341341
. '</amp-story-player>'
342342
),
343343
],
344+
345+
'preserves fetchpriority attribute from noscript fallback img' => [
346+
$input(
347+
'<amp-img width="500" height="400" src="/img1.png"><noscript><img src="/img1.png" width="500" height="400" fetchpriority="high"></noscript></amp-img>'
348+
),
349+
$output(
350+
'<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>'
351+
),
352+
],
353+
354+
'automatically ssr img element with high fetchpriority attribute from noscript fallback img even when not a hero image' => [
355+
$input(
356+
'<amp-img width="500" height="400" src="/img1.png"></amp-img>'
357+
. '<p>Hello World 1</p>'
358+
. '<p>Hello World 2</p>'
359+
. '<amp-img width="500" height="400" src="/img2.png"><noscript><img src="/img2.png" width="500" height="400" fetchpriority="high"></noscript></amp-img>'
360+
),
361+
$output(
362+
'<amp-img width="500" height="400" src="/img1.png"></amp-img>'
363+
. '<p>Hello World 1</p>'
364+
. '<p>Hello World 2</p>'
365+
. '<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>'
366+
),
367+
],
344368
];
345369
}
346370

0 commit comments

Comments
 (0)