Skip to content

Commit

Permalink
Merge pull request #135 from ampproject/fix/127-dont-preload-hero-images
Browse files Browse the repository at this point in the history
Only construct preload if it is needed
  • Loading branch information
schlessera authored Apr 8, 2021
2 parents 0a96b1d + 24871ec commit c9d317a
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/Optimizer/Transformer/PreloadHeroImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,12 @@ private function generatePreload(
$img->removeAttribute(Attribute::LOADING);
}

if (empty($heroImage->getMedia())) {
// We can only safely preload a hero image if there's a media attribute
// as we can't detect whether it's hidden on certain viewport sizes otherwise.
return;
}

if ($this->hasExistingImagePreload($document, $heroImage->getSrc())) {
return;
}
Expand All @@ -516,12 +522,6 @@ private function generatePreload(
}
}

if (empty($heroImage->getMedia())) {
// We can only safely preload a hero image if there's a media attribute
// as we can't detect whether it's hidden on certain viewport sizes otherwise.
return;
}

$preload->setAttribute(Attribute::MEDIA, $heroImage->getMedia());

if ($this->preloadReferenceNode) {
Expand Down

0 comments on commit c9d317a

Please sign in to comment.