#58211 closed defect (bug) (fixed)
Conditionally skip lazy-loading on images before the loop in classic themes
Reported by: | flixos90 | Owned by: | flixos90 |
---|---|---|---|
Milestone: | 6.3 | Priority: | high |
Severity: | normal | Version: | 5.9 |
Component: | Media | Keywords: | has-patch, has-unit-tests, has-testing-info, has-dev-note |
Focuses: | performance | Cc: |
Description
[55318] includes an enhancement which for block themes avoids lazy-loading images that appear in the header
block template part. While that resolves a common performance problem with lazy-loaded images in the header of a page for block themes, sites using a classic theme still run into this problem.
Specifically, in classic themes today only images that are rendered while in_the_loop()
are eligible to have lazy-loading skipped on them. This ticket aims to introduce conditions to make images eligible for skipping lazy-loading that are included before the loop.
We can fix the problem by updating wp_get_loading_attr_default()
to omit lazy-loading / return false
if all of the following applies:
$context
is either "the_post_thumbnail" or "wp_get_attachment_image"- the
get_header
action has already run - the main query loop has not started yet
- the
get_footer
action has not yet run
This effectively results in images in the header but before the main query loop to not be lazy-loaded. In cases where a special page template does not contain a query loop, the get_footer
action acts as a "fallback" to ensure that footer images are still lazy-loaded.
Attachments (2)
Change History (20)
This ticket was mentioned in Slack in #core-media by antpb. View the logs.
18 months ago
This ticket was mentioned in PR #4424 on WordPress/wordpress-develop by kt-12.
18 months ago
#3
- Keywords has-patch has-unit-tests added; needs-patch needs-unit-tests removed
Trac ticket: https://core.trac.wordpress.org/ticket/58211
@mukesh27 commented on PR #4424:
17 months ago
#4
@kt-12 Can you please resolve the conflict?
@flixos90 commented on PR #4424:
17 months ago
#6
@kt-12 Now that https://core.trac.wordpress.org/changeset/55825 was committed, I updated the code here just to reuse the same condition ( 'the_post_thumbnail' === $context || 'wp_get_attachment_image' === $context )
that was already present from that commit.
It also makes the condition here a bit lighter as it's no longer all in one.
There's one outstanding comment thread in https://github.com/WordPress/wordpress-develop/pull/4424#discussion_r1196898347, please take a look and let us know your thoughts on it.
#7
@
17 months ago
- Keywords has-testing-info needs-testing added
Instructions to reproduce / test the PR https://github.com/WordPress/wordpress-develop/pull/4424:
- Use a classic theme that renders a post's featured image before the main loop in its
single.php
template (e.g. Twenty Nineteen or Twenty Seventeen). - Create a post with a featured image.
- View that post (in its single view, not an archive) in the frontend in
trunk
: Inspect the featured image and see how it hasloading="lazy"
, which it shouldn't. - Load the branch from the PR and reload the URL: Now, the featured image should no longer have
loading="lazy"
on it.
#8
@
17 months ago
Test Report
Patch tested: https://patch-diff.githubusercontent.com/raw/WordPress/wordpress-develop/pull/4424.diff
Expected Results
When reproducing a bug:
- ❌
loading="lazy"
should appear on the featured image.
When testing a patch to validate it works as expected:
- ✅
loading="lazy"
should not appear on the featured image.
Environment
- WordPress: 6.3-alpha-55505
- PHP: 7.4.33
- Server: Apache/2.4.56 (Ubuntu)
- Database: mysqli (Server: 5.7.41-0ubuntu0.18.04.1 / Client: mysqlnd 7.4.33)
- Browser: Chrome 113.0.0.0 (Windows 10/11)
- Theme: Twenty Nineteen 2.5
- MU-Plugins: None activated
- Plugins: None activated
Actual Results
When reproducing a bug/defect:
- ❌
loading="lazy"
appeared on the featured image. Issue reproduced.
When testing the bugfix patch:
- ✅
loading="lazy"
did not appear on the featured image. Issue resolved with the patch.
#9
@
17 months ago
Test Report
This report validates that the indicated patch addresses the issue.
Patch tested: https://patch-diff.githubusercontent.com/raw/WordPress/wordpress-develop/pull/4424.diff
Environment
- OS: Windows 11 (22H2)
- Web Server: nginx/1.23.4
- PHP: 7.4.33
- WordPress: 6.3-alpha-55505-src
- Browser: Chrome Version 113.0.5672.126 (Official Build) (64-bit)
- Theme: Twenty Nineteen 2.5
- Plugins: None Activated
Before Applying The Patch
- Followed the instructions of @flixos90
- ❌ loading="lazy" - does show up when inspecting the featured image
After Applying The Patch
- Followed the same instructions
- ✅ loading="lazy" - does not show up when inspecting the featured image
@mukesh27 commented on PR #4424:
17 months ago
#10
17 months ago
#11
@kt-12 #4424 (review) is pending?
@mukeshpanchal27 thank you for pointing this out. I have added 3 new test cases to test $before_loop
cc: @felixarntz
#12
@
17 months ago
- Keywords commit added; needs-testing removed
- Owner set to flixos90
- Status changed from new to reviewing
@flixos90 commented on PR #4424:
17 months ago
#14
Committed in https://core.trac.wordpress.org/changeset/55847
Regarding https://github.com/WordPress/wordpress-develop/pull/4424#discussion_r1200848743, if the preference is to update this, I'm happy to do a quick follow up commit.
Addressing the remaining lazy-loading related performance issues is a high priority for the performance team for the 6.3 cycle.