diff --git a/src/Optimizer/Transformer/PreloadHeroImage.php b/src/Optimizer/Transformer/PreloadHeroImage.php index 0b8aaf9ae..73c68e63b 100644 --- a/src/Optimizer/Transformer/PreloadHeroImage.php +++ b/src/Optimizer/Transformer/PreloadHeroImage.php @@ -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; } @@ -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) {