Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Media: Automatically add
fetchpriority="high"
to hero image to impr…
…ove load time performance. This changeset adds support for the `fetchpriority` attribute, which is typically added to a single image in each HTML response with a value of "high". This enhances load time performance (also Largest Contentful Paint, or LCP) by telling the browser to prioritize this image for downloading even before the layout of the page has been computed. In lab tests, this has shown to improve LCP performance by ~10% on average. Specifically, `fetchpriority="high"` is added to the first image that satisfies all of the following conditions: * The image is not lazy-loaded, i.e. does not have `loading="lazy"`. * The image does not already have a (conflicting) `fetchpriority` attribute. * The size of of the image (i.e. width * height) is greater than 50,000 squarepixels. While these heuristics are based on several field analyses, there will always be room for optimization. Sites can customize the squarepixel threshold using a new filter `wp_min_priority_img_pixels` which should return an integer for the value. Since the logic for adding `fetchpriority="high"` is heavily intertwined with the logic for adding `loading="lazy"`, yet the features should work decoupled from each other, the majority of code changes in this changeset is refactoring of the existing lazy-loading logic to be reusable. For this purpose, a new function `wp_get_loading_optimization_attributes()` has been introduced which returns an associative array of performance-relevant attributes for a given HTML element. This function replaces `wp_get_loading_attr_default()`, which has been deprecated. As another result of that change, a new function `wp_img_tag_add_loading_optimization_attrs()` replaces the more specific `wp_img_tag_add_loading_attr()`, which has been deprecated as well. See https://make.wordpress.org/core/2023/05/02/proposal-for-enhancing-lcp-image-performance-with-fetchpriority/ for the original proposal and additional context. Props thekt12, joemcgill, spacedmonkey, mukesh27, costdev, 10upsimon. Fixes #58235. git-svn-id: https://develop.svn.wordpress.org/trunk@56037 602fd350-edb4-49c9-b593-d223f7449a82
- Loading branch information