diff --git a/src/Optimizer/Transformer/OptimizeHeroImages.php b/src/Optimizer/Transformer/OptimizeHeroImages.php
index 0d22983b2..6edc71f86 100644
--- a/src/Optimizer/Transformer/OptimizeHeroImages.php
+++ b/src/Optimizer/Transformer/OptimizeHeroImages.php
@@ -202,7 +202,11 @@ private function findHeroImages(Document $document, $maxHeroImageCount)
$node = $document->body;
while ($node !== null) {
- if (! $node instanceof Element) {
+ if (
+ ! $node instanceof Element
+ ||
+ ( $node->parentNode instanceof Element && $node->parentNode->tagName === Tag::NOSCRIPT )
+ ) {
$node = NodeWalker::nextNode($node);
continue;
}
diff --git a/tests/Optimizer/Transformer/OptimizeHeroImagesTest.php b/tests/Optimizer/Transformer/OptimizeHeroImagesTest.php
index 4988620cf..76429a7ae 100644
--- a/tests/Optimizer/Transformer/OptimizeHeroImagesTest.php
+++ b/tests/Optimizer/Transformer/OptimizeHeroImagesTest.php
@@ -249,6 +249,15 @@ public function dataTransform()
),
],
+ 'hero image with noscript fallback' => [
+ $input(
+ ''
+ ),
+ $output(
+ ''
+ ),
+ ],
+
'superfluous candidates are ignored without throwing an error' => [
$input(
''