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

Conversation

thelovekesh
Copy link
Collaborator

@thelovekesh thelovekesh marked this pull request as draft October 20, 2023 16:03
@@ -555,6 +555,7 @@ private function generateImg(HeroImage $heroImage, Document $document)
$imgElement = $document->createElement(Tag::IMG);
$imgElement->setAttribute(Attribute::CLASS_, self::SSR_IMAGE_CLASS);
$imgElement->setAttribute(Attribute::DECODING, 'async');
$imgElement->setAttribute(Attribute::FETCHPRIORITY, 'high');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking about this some more now... perhaps we actually shouldn't automatically add fetchpriority=high to all hero images, since there can be multiple. The fetchpriority=high attribute should be reserved for the LCP image. So I think instead of automatically adding it to all heros, we should instead only it when the amp-img > noscript > img contains fetchpriority=high.

Additionally, we should automatically SSR any amp-img that has a noscript > img with fetchpriority=high.

But if an amp-img has data-hero alone, that wouldn't necessarily warrant adding fetchpriority=high.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking about this some more now... perhaps we actually shouldn't automatically add fetchpriority=high to all hero images, since there can be multiple.

Completely agree here and I have updated it per suggestion.

@thelovekesh thelovekesh force-pushed the feature/fetchpriority-on-hero-images branch from e757cc1 to 3852f92 Compare October 24, 2023 16:28
Copy link
Member

@westonruter westonruter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

Comment on lines 225 to 234
if ($node->tagName === Extension::IMG && ! $node->hasAttribute(Attribute::DATA_HERO)) {
$highFetchpriorityImage = $document->xpath->query(
self::NOSCRIPT_IMG_FETCHPRIORITY_HIGH_XPATH_QUERY,
$node
)->item(0);

if ($highFetchpriorityImage instanceof Element) {
$node->setAttribute(Attribute::DATA_HERO, '');
}
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this logic in place, we won't need to add data-hero to images with high fetch priority in AMP Image sanitizer.

Comment on lines +345 to +353
'preserves fetchpriority attribute from noscript fallback img' => [
$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>'
),
],

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am I right that this test is somewhat redundant with the second?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the test cases in 16b9f66

Comment on lines 356 to 358
'<amp-img width="500" height="400" src="/img1.png"><noscript><img src="/img1.png" width="500" height="400" fetchpriority="high"></noscript></amp-img>'
. '<amp-img width="500" height="400" src="/img2.png"><noscript><img src="/img2.png" width="500" height="400"></noscript></amp-img>'
. '<amp-img width="500" height="400" src="/img3.png"><noscript><img src="/img3.png" width="500" height="400" fetchpriority="high"></noscript></amp-img>'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the second image, how about adding data-hero to it to see what happens when preceded by a fetchpriority=high image, and then the third image can have neither data-hero nor fetchpriority (as really the source markup shouldn't have multiple fetchpriority=high.

src/Optimizer/Transformer/OptimizeHeroImages.php Outdated Show resolved Hide resolved
@thelovekesh thelovekesh marked this pull request as ready for review October 24, 2023 20:33
@westonruter westonruter added this to the 0.11.3 milestone Oct 24, 2023
Copy link
Member

@westonruter westonruter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

@westonruter westonruter merged commit c79a0fe into main Oct 24, 2023
18 of 19 checks passed
@westonruter westonruter deleted the feature/fetchpriority-on-hero-images branch October 24, 2023 22:47
@westonruter westonruter changed the title Add fetchpriority=high on hero images Promote noscript fallback images with fetchpriority=high to hero and automatically SSR Oct 24, 2023
@westonruter westonruter modified the milestones: 0.11.3, 0.11.4 Oct 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants