Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Promote noscript fallback images with fetchpriority=high to hero and automatically SSR #544

Merged
merged 7 commits into from
Oct 24, 2023
1 change: 1 addition & 0 deletions src/Html/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ interface Attribute
const EXTRA_SPACE = 'extra-space';
const FALLBACK = 'fallback';
const FETCH_ERROR = 'fetch-error';
const FETCHPRIORITY = 'fetchpriority';
const FILL = 'fill';
const FILL_OPACITY = 'fill-opacity';
const FILL_RULE = 'fill-rule';
Expand Down
8 changes: 8 additions & 0 deletions src/Optimizer/Transformer/OptimizeHeroImages.php
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,14 @@ private function generateImg(HeroImage $heroImage, Document $document)
$imgElement->setAttribute(Attribute::LOADING, $noscript_img->getAttribute(Attribute::LOADING));
}

// Preserve the original fetchpriority attribute from the noscript fallback img.
if ($noscript_img->hasAttribute(Attribute::FETCHPRIORITY)) {
$imgElement->setAttribute(
Attribute::FETCHPRIORITY,
$noscript_img->getAttribute(Attribute::FETCHPRIORITY)
);
}

// Remove any noscript>img when an amp-img is pre-rendered.
$noscript_img->parentNode->parentNode->removeChild($noscript_img->parentNode);
} elseif (! $this->isMarkedAsHeroImage($element)) {
Expand Down
9 changes: 9 additions & 0 deletions tests/Optimizer/Transformer/OptimizeHeroImagesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,15 @@ public function dataTransform()
. '</amp-story-player>'
),
],

'preserves fetchpriority attribute from noscript fallback img on prerendered image' => [
$input(
'<amp-img width="500" height="400" src="/img1.png"><noscript><img src="/img1.png" width="500" height="400" fetchpriority="high"></noscript></amp-img>'
),
$output(
'<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>'
),
],
];
}

Expand Down