Skip to content

Commit fe02ccc

Browse files
authored
Merge pull request #136 from ampproject/fix/check-hero-image-media-before-srcset
2 parents a4a2b9d + d6dfe43 commit fe02ccc

File tree

2 files changed

+44
-20
lines changed

2 files changed

+44
-20
lines changed

src/Optimizer/Transformer/PreloadHeroImage.php

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ public function transform(Document $document, ErrorCollection $errors)
181181
}
182182

183183
for ($index = 0; $index < $heroImageCount; $index++) {
184+
$this->removeLazyLoading($heroImages[$index]);
184185
$this->generatePreload($heroImages[$index], $document, $errors);
185186
$this->generateImg($heroImages[$index], $document);
186187
}
@@ -470,22 +471,12 @@ private function getPlaceholderImage(Element $element)
470471
}
471472

472473
/**
473-
* Generate the preload link for a given hero image.
474+
* Remove the lazy loading from the hero image.
474475
*
475-
* @param HeroImage $heroImage Hero image to generate the preload link for.
476-
* @param Document $document Document to generate the preload link in.
477-
* @param ErrorCollection $errors Collection of errors that are collected during transformation.
476+
* @param HeroImage $heroImage Hero image to remove the lazy loading for.
478477
*/
479-
private function generatePreload(
480-
HeroImage $heroImage,
481-
Document $document,
482-
ErrorCollection $errors
483-
) {
484-
if ($heroImage->getSrcset() && ! $this->supportsSrcset()) {
485-
$errors->add(Error\CannotPreloadImage::fromImageWithSrcsetAttribute($heroImage->getAmpImg()));
486-
return;
487-
}
488-
478+
private function removeLazyLoading(HeroImage $heroImage)
479+
{
489480
$img = $heroImage->getAmpImg();
490481

491482
if (
@@ -495,13 +486,28 @@ private function generatePreload(
495486
) {
496487
$img->removeAttribute(Attribute::LOADING);
497488
}
489+
}
498490

491+
/**
492+
* Generate the preload link for a given hero image.
493+
*
494+
* @param HeroImage $heroImage Hero image to generate the preload link for.
495+
* @param Document $document Document to generate the preload link in.
496+
* @param ErrorCollection $errors Collection of errors that are collected during transformation.
497+
*/
498+
private function generatePreload(HeroImage $heroImage, Document $document, ErrorCollection $errors)
499+
{
499500
if (empty($heroImage->getMedia())) {
500501
// We can only safely preload a hero image if there's a media attribute
501502
// as we can't detect whether it's hidden on certain viewport sizes otherwise.
502503
return;
503504
}
504505

506+
if ($heroImage->getSrcset() && ! $this->supportsSrcset()) {
507+
$errors->add(Error\CannotPreloadImage::fromImageWithSrcsetAttribute($heroImage->getAmpImg()));
508+
return;
509+
}
510+
505511
if ($this->hasExistingImagePreload($document, $heroImage->getSrc())) {
506512
return;
507513
}
@@ -517,6 +523,7 @@ private function generatePreload(
517523
$preload->appendChild($document->createAttribute(Attribute::DATA_HERO));
518524
if ($heroImage->getSrcset()) {
519525
$preload->setAttribute(Attribute::IMAGESRCSET, $heroImage->getSrcset());
526+
$img = $heroImage->getAmpImg();
520527
if ($img && $img->hasAttribute(Attribute::SIZES)) {
521528
$preload->setAttribute(Attribute::IMAGESIZES, $img->getAttribute(Attribute::SIZES));
522529
}

tests/Optimizer/Transformer/PreloadHeroImageTest.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,26 +98,43 @@ public function dataTransform()
9898

9999
'throws error when scrset detected on image to be preloaded' => [
100100
$input(
101-
'<amp-img data-hero width="500" height="400" src="https://example-com.cdn.ampproject.org/hero1.png" srcset="test 100w test2 3dpr"></amp-img>'
102-
. '<amp-img data-hero width="500" height="400" src="https://example-com.cdn.ampproject.org/hero2.png"></amp-img>'
101+
'<amp-img data-hero media="(min-width: 250px)" width="500" height="400" src="https://example-com.cdn.ampproject.org/hero1.png" srcset="test 100w test2 3dpr"></amp-img>'
102+
. '<amp-img data-hero media="(min-width: 250px)" width="500" height="400" src="https://example-com.cdn.ampproject.org/hero2.png"></amp-img>'
103103
),
104104
$output(
105-
'<amp-img data-hero width="500" height="400" i-amphtml-ssr src="https://example-com.cdn.ampproject.org/hero1.png" srcset="test 100w test2 3dpr">'
105+
'<amp-img data-hero media="(min-width: 250px)" width="500" height="400" i-amphtml-ssr src="https://example-com.cdn.ampproject.org/hero1.png" srcset="test 100w test2 3dpr">'
106106
. '<img class="i-amphtml-fill-content i-amphtml-replaced-content" decoding="async" src="https://example-com.cdn.ampproject.org/hero1.png" srcset="test 100w test2 3dpr">'
107107
. '</amp-img>'
108-
. '<amp-img data-hero width="500" height="400" i-amphtml-ssr src="https://example-com.cdn.ampproject.org/hero2.png">'
108+
. '<amp-img data-hero media="(min-width: 250px)" width="500" height="400" i-amphtml-ssr src="https://example-com.cdn.ampproject.org/hero2.png">'
109109
. '<img class="i-amphtml-fill-content i-amphtml-replaced-content" decoding="async" src="https://example-com.cdn.ampproject.org/hero2.png">'
110-
. '</amp-img>'
110+
. '</amp-img>',
111+
'<link as="image" data-hero href="https://example-com.cdn.ampproject.org/hero2.png" media="(min-width: 250px)" rel="preload">'
111112
),
112113
[
113114
Error\CannotPreloadImage::fromImageWithSrcsetAttribute(
114115
Document::fromHtmlFragment(
115-
'<amp-img data-hero width="500" height="400" src="https://example-com.cdn.ampproject.org/hero1.png" srcset="test 100w test2 3dpr"></amp-img>'
116+
'<amp-img data-hero media="(min-width: 250px)" width="500" height="400" src="https://example-com.cdn.ampproject.org/hero1.png" srcset="test 100w test2 3dpr"></amp-img>'
116117
)->body->firstChild
117118
),
118119
],
119120
],
120121

122+
'preloads scrset image when configured to do so' => [
123+
$input(
124+
'<amp-img data-hero media="(min-width: 250px)" width="500" height="400" src="https://example-com.cdn.ampproject.org/hero.png" srcset="test 100w test2 3dpr" sizes="100vw"></amp-img>'
125+
),
126+
$output(
127+
'<amp-img data-hero media="(min-width: 250px)" width="500" height="400" i-amphtml-ssr src="https://example-com.cdn.ampproject.org/hero.png" srcset="test 100w test2 3dpr" sizes="100vw">'
128+
. '<img class="i-amphtml-fill-content i-amphtml-replaced-content" decoding="async" src="https://example-com.cdn.ampproject.org/hero.png" srcset="test 100w test2 3dpr" sizes="100vw">'
129+
. '</amp-img>',
130+
'<link as="image" data-hero href="https://example-com.cdn.ampproject.org/hero.png" imagesizes="100vw" imagesrcset="test 100w test2 3dpr" media="(min-width: 250px)" rel="preload">'
131+
),
132+
[],
133+
[
134+
PreloadHeroImageConfiguration::PRELOAD_SRCSET => true,
135+
]
136+
],
137+
121138
'fetches placeholders for animations' => [
122139
$input(
123140
'<amp-anim data-hero width="500" height="400" src="/foo.gif">'

0 commit comments

Comments
 (0)