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

Stop lazy loading of content images above the fold to improve LCP #72

Merged
merged 2 commits into from
Aug 15, 2022

Conversation

adamwoodnz
Copy link
Contributor

@adamwoodnz adamwoodnz commented Aug 12, 2022

Partially fixes #45

Content images which are above the fold shouldn't be lazy loaded as it affects the Largest Contentful Paint of the page, a key performance metric.

Screen Shot 2022-08-12 at 2 50 17 PM

This PR adds a filter to skip X number of images as outlined on https://make.wordpress.org/core/2021/12/29/enhanced-lazy-loading-performance-in-5-9/, however the filter doesn't fire, @tellyworth suggested perhaps related to the content being in a pattern. Not sure if this being page content rather than post content makes a difference either.

The image in question is actually the first content image in the page, so if I understand correctly it should not be lazy loaded by default. It seems we're doing something differently to the way this optimization expects.

Screenshots

Before After
image image

How to test the changes in this Pull Request:

@adamwoodnz adamwoodnz added the [Component] Theme Templates, patterns, CSS label Aug 12, 2022
@adamwoodnz adamwoodnz marked this pull request as draft August 12, 2022 03:51
*/
function skip_lazyloading_on_homepage_images( $omit_threshold ) {
if ( is_home() ) {
return 2;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Draft value

@adamwoodnz adamwoodnz requested a review from tellyworth August 12, 2022 03:53
@adamwoodnz adamwoodnz force-pushed the fix/45-LCP-image-lazy-loading branch from 9e19cad to 6a22ec2 Compare August 12, 2022 03:54
@adamwoodnz adamwoodnz requested a review from ryelle August 12, 2022 05:07
@adamwoodnz adamwoodnz force-pushed the fix/45-LCP-image-lazy-loading branch from 6a22ec2 to 566b6e4 Compare August 12, 2022 05:09
@adamwoodnz adamwoodnz self-assigned this Aug 12, 2022
@adamwoodnz adamwoodnz force-pushed the fix/45-LCP-image-lazy-loading branch from 566b6e4 to f7d0102 Compare August 12, 2022 05:54
@ryelle ryelle force-pushed the fix/45-LCP-image-lazy-loading branch from f7d0102 to e7a6e65 Compare August 12, 2022 17:50
@ryelle
Copy link
Contributor

ryelle commented Aug 12, 2022

The first issue I ran into is that the lazy attribute is only added to images on the current site, so my local site wasn't adding it because it was pulled from main-test. I had to add it to my local site & swap the pattern code. In case anyone else runs into that :)

I tracked how that threshold value is used, and it's only used if the image is in the content, otherwise it defaults to lazy. In our case, context is false, so we return before even trying the threshold.

// Only elements with 'the_content' or 'the_post_thumbnail' context have special handling.
if ( 'the_content' !== $context && 'the_post_thumbnail' !== $context ) {
	return 'lazy';
}

(source)

So instead, I tried filtering the attribute itself when it's added, with wp_img_tag_add_loading_attr. This doesn't give us any context, so instead I'm checking for the file name and disabling lazy-loading for this specific image. We could check for other things, like the class name, but then that would affect all cover blocks…

The value of $image is just the image tag (whitespace added for readability).

<img
	width="2560"
	height="1305"
	class="wp-block-cover__image-background wp-image-9084"
	alt=""
	src="http://localhost:8888/wp-content/uploads/2022/08/editor-bg-scaled-1.webp"
	data-object-fit="cover"
	srcset="
		http://localhost:8888/wp-content/uploads/2022/08/editor-bg-scaled-1.webp           2560w,
		http://localhost:8888/wp-content/uploads/2022/08/editor-bg-scaled-1-300x153.webp    300w,
		http://localhost:8888/wp-content/uploads/2022/08/editor-bg-scaled-1-1024x522.webp  1024w,
		http://localhost:8888/wp-content/uploads/2022/08/editor-bg-scaled-1-768x392.webp    768w,
		http://localhost:8888/wp-content/uploads/2022/08/editor-bg-scaled-1-1536x783.webp  1536w,
		http://localhost:8888/wp-content/uploads/2022/08/editor-bg-scaled-1-2048x1044.webp 2048w
	"
	sizes="(max-width: 2560px) 100vw, 2560px"
/>

Anyway, I think e7a6e65 is a workaround we can launch with, and we can think about a future-safe solution later.

@ryelle ryelle marked this pull request as ready for review August 12, 2022 18:10
@ryelle
Copy link
Contributor

ryelle commented Aug 12, 2022

Lighthouse says the Largest Contentful Paint is now another section, and it doesn't flag it.

Screen Shot 2022-08-12 at 1 50 47 PM

@ryelle ryelle removed their request for review August 12, 2022 18:11
@ryelle ryelle added [Component] Backend Anything wp-admin or PHP-related and removed [Component] Theme Templates, patterns, CSS labels Aug 12, 2022
@ryelle ryelle mentioned this pull request Aug 12, 2022
7 tasks
@tellyworth
Copy link
Contributor

So instead, I tried filtering the attribute itself when it's added, with wp_img_tag_add_loading_attr. This doesn't give us any context, so instead I'm checking for the file name and disabling lazy-loading for this specific image.

Nice one Kelly! That's exactly where I was trying to get to, I hadn't found that particular filter though.

This is a good workaround for now, @adamwoodnz let's merge it if it works.

As a more flexible solution going forward, instead of string-matching the file name, could we use something like a special classname or other attribute on the image to trigger it? That way it could be more explicitly controlled in the template rather than hidden away in a magic filter. Definitely not a blocker for merging though.

@adamwoodnz
Copy link
Contributor Author

This is a good workaround for now, @adamwoodnz let's merge it if it works.

👍 testing in progress

Copy link
Contributor Author

@adamwoodnz adamwoodnz left a comment

Choose a reason for hiding this comment

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

LGTM, couldn't get a local image working, but tested in Sandbox

@adamwoodnz
Copy link
Contributor Author

could we use something like a special classname or other attribute on the image to trigger it

I think we could apply a image-no-lazy or similar class to the image in the pattern, but it's not possible to add a class directly to the image through the editor as far as I can see. Couldn't get this to work in my sandbox as the pattern files don't see to get used like they do locally.

@adamwoodnz adamwoodnz merged commit c8166b8 into trunk Aug 15, 2022
@adamwoodnz adamwoodnz deleted the fix/45-LCP-image-lazy-loading branch August 15, 2022 03:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Component] Backend Anything wp-admin or PHP-related
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Performance: front-end
3 participants