Prevent SSR for amp-img occurring after second paragraph#1196
Merged
sebastianbenz merged 2 commits intoampproject:mainfrom Apr 9, 2021
Merged
Conversation
| // the page performance even worse by eagerly loading an image outside the viewport. | ||
| if (!('data-hero' in node.attribs)) { | ||
| node.attribs['data-hero'] = ''; | ||
| imgNode.attribs['loading'] = 'lazy'; |
Collaborator
There was a problem hiding this comment.
I really like this approach. Have you checked if it is valid AMP?
Member
Author
There was a problem hiding this comment.
Yes, it is. The loading attribute is allowed, and it can be either lazy or eager: https://github.com/ampproject/amphtml/blob/a8889ac1b2622c461422a2186366f7a433848f44/validator/validator-main.protoascii#L3834-L3838
Collaborator
There was a problem hiding this comment.
Seems to be, otherwise the end-to-end spec would fail.
sebastianbenz
approved these changes
Apr 9, 2021
Collaborator
sebastianbenz
left a comment
There was a problem hiding this comment.
This is great! Thanks Weston!
This was referenced Apr 9, 2021
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
As pointed out by @jono-alderson, hero image prerendering can be detrimental when there are no hero images in the first viewport, as seen in these two examples from amp-wp.org:
This is especially problematic because
OptimizeHeroImagedoes not includeloading=lazyon the SSR'ed image. The result is that an optimized page may actually perform worse.This PR mitigates this issue by only doing hero image prerendering for images that appear before the second paragraph in the page.
Another mitigation measure is to add
loading=lazyto any SSR'ed hero image that was automatically detected. If a document haddata-heroon an image up front then theloading=lazywould remain omitted since the author is explicitly assuring the optimizer that the image should indeed be in the first viewport.