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

Header image fails to be prerendered #5824

Closed
westonruter opened this issue Feb 1, 2021 · 3 comments · Fixed by #5934
Closed

Header image fails to be prerendered #5824

westonruter opened this issue Feb 1, 2021 · 3 comments · Fixed by #5934
Labels
Bug Something isn't working Groomed P0 High priority WS:Perf Work stream for Metrics, Performance and Optimizer
Milestone

Comments

@westonruter
Copy link
Member

Bug Description

I've found that header images (via custom-header theme support) fail to get pre-rendered when there is a custom logo or featured image also on the page. For example, in themes that render the header image with an img as opposed to a background-image, the presence of a featured image in the page will prevent the header image from getting SSR'ed, as any presence of data-hero will prevent any hero-image candidate detection (via PreloadHeroImage::detectHeroImageCandidate) from running.

In all four examples below, the header image lacks any prerendering. Only the “Horizontal” featured image gets prerendering:

Twenty Twelve Twenty Fourteen Twenty Sixteen Twenty Seventeen
twentytwelve twentyfourteen twentysixteen twentyseventeen

This would have a negative impact on LCP which should be entirely avoidable because both the header image and the featured image can both be prerendered as data-hero images.

Expected Behaviour

The header image should be prerendered whenever possible.

Steps to reproduce

  1. Activate Twenty Twelve, Twenty Fourteen, Twenty Sixteen, or Twenty Seventeen.
  2. Add a header image in the Customizer.
  3. Navigate to a post that has a featured image assigned.
  4. On the AMP page, see that the featured image has data-hero whereas the header image does not.

Do not alter or remove anything below. The following sections will be managed by moderators only.

Acceptance criteria

  • When no images on a page initially have data-hero, first two non-tiny images on a page should have get prerendered, including the custom logo, header image, featured image, cover blocks, and other detected image candidates.

Implementation brief

One way to implement this is to extend \AmpProject\AmpWP\Transformer\DetermineHeroImages::transform() to also include detection for header images in the same way it is detecting the custom logo, featured image, and cover blocks. In Twenty Seventeen, this could be accomplished by injecting data-hero into the header markup like so:

add_filter(
	'get_header_image_tag',
	function ( $html ) {
		return preg_replace( '/(?<=<img\s)/', 'data-hero ', $html );
	}
);

This works in Twenty Seventeen since that theme uses the_custom_header_markup(). Unfortunately, none of the other themes use this function to output the header. They instead construct the img manually so there is no opportunity for filtering. One way to address that would be for us to add theme-specific XPath selectors to match the header image (e.g. via the core theme sanitizer), but this is not ideal as it would not automatically work for other themes.

I think the problem boils down to DetermineHeroImages injecting the data-hero attribute into images. This is causing all other candidates, such as the header image, to be omitted during PreloadHeroImage::findHeroImages(). I think there needs to be a differentiation between a user-specified data-hero which should indeed override any others, but then for our DetermineHeroImages to add data-hero-candidate attributes instead of plain data-hero attributes, so that the PreloadHeroImage can include all images that have data-hero and then also include the first one or two images that have data-hero-candidate to also have server-side-rendering.

  1. If there are two images that have (author-supplied)data-hero on the page already, then none of the data-hero-candidate images would be included.
  2. If there was one image that had data-hero, then it would get prerendered in addition to the first image that has data-hero-candidate.
  3. If there were no images that have data-hero, then the first two images that have data-hero-candidate would get prerendered.

Prerendering any image with data-hero-candidate would naturally include adding the data-hero attribute as well.

This will require changes to both the AMP plugin here and also the amp-toolbox-php project.

QA testing instructions

Demo

Changelog entry

@westonruter westonruter added Bug Something isn't working P1 Medium priority WS:Perf Work stream for Metrics, Performance and Optimizer labels Feb 1, 2021
@westonruter westonruter added this to the v2.1 milestone Feb 1, 2021
@westonruter
Copy link
Member Author

cc @schlessera

@kmyram kmyram added the Groomed label Feb 2, 2021
@westonruter
Copy link
Member Author

The issue also is present for a user who has a regular Image block in hero position: https://wordpress.org/support/topic/data-hero-for-amp-optimizer/#post-14016250

@delawski
Copy link
Collaborator

QA passed (tentatively - see the comment below).


I've tested a few scenarios on Twenty Seventeen. I each case, I've ran the following code in the dev tools console so that any img that has the data-hero attribute gets a thick red border:

document.querySelectorAll( 'amp-img[data-hero]' ).forEach( el => el.style.border = '10px solid red' );

In each test case, 2 images at most that are within a viewport were assigned the data-hero attribute.

Questions:

  • If there is no custom header, no logo, and no post thumbnail (4.b), only 1 image (within the Cover block) has been assigned the data-hero attribute. Is it expected? The same applies if relatively small images are added at the very top of the post - only one of them gets the data-hero attribute (5).
  • Whenever a logo is present (2, 3), it feels that it's relatively small compared to other images. Should it still take precedence over the first Cover block image or regular image within a post content?

1. With custom header, without logo

a) With post thumbnail b) Without post thumbnail
Screenshot 2021-04-27 at 12 21 48 Screenshot 2021-04-27 at 12 20 25

2. With custom header, with logo

a) With post thumbnail b) Without post thumbnail
Screenshot 2021-04-27 at 12 22 57 Screenshot 2021-04-27 at 12 23 17

3. Without custom header, with logo

a) With post thumbnail b) Without post thumbnail
Screenshot 2021-04-27 at 12 24 07 Screenshot 2021-04-27 at 12 23 48

4. Without custom header, without logo

a) With post thumbnail b) Without post thumbnail
Screenshot 2021-04-27 at 12 24 27 Screenshot 2021-04-27 at 12 24 50

5. Without custom header, without logo, without featured image

Screenshot 2021-04-27 at 12 43 53

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working Groomed P0 High priority WS:Perf Work stream for Metrics, Performance and Optimizer
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants