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

Prevent SSR for amp-img occurring after second paragraph #1196

Conversation

westonruter
Copy link
Member

@westonruter westonruter commented Apr 9, 2021

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:

Blog post Homepage
image image

This is especially problematic because OptimizeHeroImage does not include loading=lazy on 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=lazy to any SSR'ed hero image that was automatically detected. If a document had data-hero on an image up front then the loading=lazy would remain omitted since the author is explicitly assuring the optimizer that the image should indeed be in the first viewport.

// 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';
Copy link
Collaborator

Choose a reason for hiding this comment

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

I really like this approach. Have you checked if it is valid AMP?

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Collaborator

Choose a reason for hiding this comment

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

Seems to be, otherwise the end-to-end spec would fail.

Copy link
Collaborator

@sebastianbenz sebastianbenz left a comment

Choose a reason for hiding this comment

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

This is great! Thanks Weston!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants