Skip to content

Enhanced Responsive Images: Prime attachment caches for images outside the post content - #2707

Open
ekamran wants to merge 1 commit into
WordPress:trunkfrom
ekamran:fix/auto-sizes-prime-block-tree-caches
Open

ekamran wants to merge 1 commit into
WordPress:trunkfrom
ekamran:fix/auto-sizes-prime-block-tree-caches

Conversation

@ekamran

@ekamran ekamran commented Oct 9, 2026

Copy link
Copy Markdown

Summary

Addresses the coverage part of #2695.

auto_sizes_prime_attachment_caches() only sees the the_content string. On a block theme most images never pass through it: the template is rendered with do_blocks() in get_the_block_template_html(), each template part runs its own do_blocks(), and a synced pattern renders its blocks as the Pattern block's inner blocks. Every image block in those places loads its attachment with 2 queries while it renders.

This adds a render_block_data filter that primes the attachment caches of a whole block tree when the tree's root block is about to render. It complements #2641, which fixes the ordering for the post content. The two PRs change different lines and merge cleanly in either order.

Relevant technical choices

  • A root is a block without a parent block, or a block whose parent is a synced pattern (core/block). Since WordPress 6.9 the Pattern block no longer calls do_blocks(); it renders the pattern's blocks as its own inner blocks, so they reach render_block_data with the core/block block as their parent. The plugin requires 6.9.
  • The walk collects the id of core/image and core/cover blocks, the blocks the plugin filters that carry an attachment ID. Featured images need nothing here: core/post-template and the main loop already prime post thumbnails.
  • It primes only when a tree has 2 or more attachments, the same guard as auto_sizes_prime_attachment_caches().
  • The the_content filter stays. Post content is usually flat, so each top-level image block is its own tree, while the existing filter batches the whole post in one query.
  • The block is returned unchanged, and the rendered output is byte-identical.

Measured effect

WordPress 7.1.3, Twenty Twenty-Five, SQLite, no persistent object cache, SAVEQUERIES, second of two requests. One post with 8 images in the content, 4 in the single template, 2 in the header template part and 4 in a synced pattern; the template, part and pattern images sit inside a group.

Plugin Total queries Images loaded one by one while rendering
trunk 63 18
#2641 49 10 (template, part, pattern)
This PR 49 8 (post content, which #2641 fixes)
This PR and #2641 35 0

Rendered HTML is byte-identical in all four. No PHP warnings or notices with WP_DEBUG on.

Known limit: the walk works per tree, so image blocks that sit flat at the top level of a template, template part or pattern, outside any group or columns, are each their own tree with one image and still load one by one. With the template's 4 images unwrapped from their group, the last row becomes 41 queries with those 4 images loaded one by one. Twenty Twenty-Five's templates and parts have no image blocks at the top level; their content sits in groups.

Testing

New tests/test-prime-block-tree-attachment-caches.php: images in a group, nested image and cover blocks, a synced pattern, a tree with a single image that does not prime, a nested block that does not prime, and the block returned unchanged. The rendering tests record, right before each block renders, whether its post and meta caches are already warm.

Fail-first: on trunk, the registration test and the three rendering tests fail on their assertions, because the caches are still cold when the image blocks render. With the function present but the hook removed, the same four still fail and the direct calls pass. Six mutants of the function (the 2-attachment guard, the root rule, synced-pattern roots, the walk into inner blocks, cover blocks, and returning the block unchanged) are each caught by a test.

Auto-sizes suite in wp-env, PHP 8.3:

Core Single site Multisite
6.9.4 176 tests, OK 176 tests, OK
7.1.3 176 tests, OK 176 tests, OK
7.2-alpha 176 tests, OK 176 tests, OK

Merged with #2641: 178 tests, OK on single site and multisite.

PHPCS is clean and PHPStan reports no errors in the plugin.

Use of AI Tools

AI assistance: Yes. Tool(s): Claude Code and Codex. Used for: research, implementation, tests, measurements and PR wording. I reviewed the reasoning and the results, and I take responsibility for the contribution.

Images in block templates, template parts and synced patterns never pass
through the_content, so auto_sizes_prime_attachment_caches() does not see
them and each image block loads its attachment with separate queries while
it renders.

Add a render_block_data filter that, when the root block of a tree is about
to render, collects the attachment IDs of the image and cover blocks in the
whole tree and primes them with one query. A root is a block without a
parent, or a block whose parent is a synced pattern: since WordPress 6.9 the
Pattern block renders the pattern's blocks as its own inner blocks.
@github-actions

github-actions Bot commented Oct 9, 2026

Copy link
Copy Markdown

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: ekamran <ekamran@git.wordpress.org>

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

@github-actions github-actions Bot added the [Plugin] Enhanced Responsive Images Formerly "Auto Sizes" [slug: auto-sizes] label Oct 9, 2026
@codecov

codecov Bot commented Oct 9, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 70.56%. Comparing base (a25f804) to head (55b99e6).
⚠️ Report is 2 commits behind head on trunk.

Additional details and impacted files
@@            Coverage Diff             @@
##            trunk    #2707      +/-   ##
==========================================
+ Coverage   70.50%   70.56%   +0.05%     
==========================================
  Files          91       91              
  Lines        7869     7885      +16     
==========================================
+ Hits         5548     5564      +16     
  Misses       2321     2321              
Flag Coverage Δ
multisite 70.56% <100.00%> (+0.05%) ⬆️
single 35.47% <100.00%> (+0.13%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Plugin] Enhanced Responsive Images Formerly "Auto Sizes" [slug: auto-sizes]

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant