Repository navigation
Conversation
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.
|
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 If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Codecov Report✅ All modified and coverable lines are covered by tests. 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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Summary
Addresses the coverage part of #2695.
auto_sizes_prime_attachment_caches()only sees thethe_contentstring. On a block theme most images never pass through it: the template is rendered withdo_blocks()inget_the_block_template_html(), each template part runs its owndo_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_datafilter 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
core/block). Since WordPress 6.9 the Pattern block no longer callsdo_blocks(); it renders the pattern's blocks as its own inner blocks, so they reachrender_block_datawith thecore/blockblock as their parent. The plugin requires 6.9.idofcore/imageandcore/coverblocks, the blocks the plugin filters that carry an attachment ID. Featured images need nothing here:core/post-templateand the main loop already prime post thumbnails.auto_sizes_prime_attachment_caches().the_contentfilter 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.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.trunkRendered HTML is byte-identical in all four. No PHP warnings or notices with
WP_DEBUGon.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:
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.