Skip to content

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

Merged
sebastianbenz merged 2 commits intoampproject:mainfrom
westonruter:fix/hero-image-rendering-after-paragraphs
Apr 9, 2021
Merged

Prevent SSR for amp-img occurring after second paragraph#1196
sebastianbenz merged 2 commits intoampproject:mainfrom
westonruter:fix/hero-image-rendering-after-paragraphs

Conversation

@westonruter
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
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
Copy Markdown
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