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

Remove erroneous check for resource initiator type when considering whether to submit LCP background image #1760

Merged
merged 2 commits into from
Dec 19, 2024

Conversation

westonruter
Copy link
Member

Summary

When setting up a demo site with Optimization Detective, I was confused when I wasn't getting the lcpElementExternalBackgroundImage data submitted with the URL Metric. In looking at the console, I saw:

image

This was strange because the LCP element was definitely using a background-image coming from a stylesheet:

image

Nevertheless, the initiatorType of the resource was coming through as an img and not as css. Then I looked further down the page and I found the same image was being used in an IMG tag:

image

This actually reveals an interesting performance fact: even though the image is being used as a background image for the LCP element, there is an image way further down the page which is actually initiating the loading of this image before the stylesheet is initiating the load. Even more surprising, this IMG has loading=lazy. This highlights the need for adding a preload link for this image URL so that it is properly prioritized!

Granted, it may be unlikely for the exact same image used as the LCP element's background to also appear further down the page in non-example content. In any case, checking the initiatorType for the resource was redundant because it's already aborting if the LCP element is an IMG tag:

// Look only for LCP entries that have a URL and a corresponding element which is not an IMG or VIDEO.
if (
! entry.url ||
! ( entry.element instanceof HTMLElement ) ||
entry.element instanceof HTMLImageElement ||
entry.element instanceof HTMLVideoElement
) {
continue;
}

Therefore, there is no need for the additional check.

@westonruter westonruter added [Type] Bug An existing feature is broken [Plugin] Image Prioritizer Issues for the Image Prioritizer plugin (dependent on Optimization Detective) labels Dec 19, 2024
@westonruter westonruter added this to the image-prioritizer n.e.x.t milestone Dec 19, 2024
Copy link

github-actions bot commented Dec 19, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: westonruter <[email protected]>
Co-authored-by: felixarntz <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

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

@westonruter Great catch!

@westonruter westonruter merged commit d0299a3 into trunk Dec 19, 2024
8 checks passed
@westonruter westonruter deleted the fix/erroneous-initiator-type branch December 19, 2024 14:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Plugin] Image Prioritizer Issues for the Image Prioritizer plugin (dependent on Optimization Detective) [Type] Bug An existing feature is broken
Projects
Status: Done 😃
Development

Successfully merging this pull request may close these issues.

2 participants