Enhanced Responsive Images: Fix cache priming running after block rendering - #2641
Enhanced Responsive Images: Fix cache priming running after block rendering#2641ekamran wants to merge 2 commits into
Conversation
The `auto_sizes_prime_attachment_caches` callback is registered on `the_content` at priority 9, which is the same priority core uses for `do_blocks`. Callbacks sharing a priority run in registration order, and core registers `do_blocks` in `default-filters.php` while `wp-settings.php` loads, so it is always registered before any plugin. The priming callback therefore runs after `do_blocks` every time and never warms the cache in time to be useful. The result is that block rendering looks up each attachment individually. On a cold cache the query count scales with the number of images, 2N + 1 for N images, rather than staying flat. Priority 8 was requested and agreed during review of WordPress#1625, but 9 was committed along with the accompanying comment. This restores 8, which places the callback after the block hooks and embed callbacks that also run at priority 8, and before `do_blocks` at priority 9. Core uses the same slot for this purpose. Rendered output is unchanged.
|
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 #2641 +/- ##
=======================================
Coverage 70.35% 70.35%
=======================================
Files 91 91
Lines 7867 7867
=======================================
Hits 5535 5535
Misses 2332 2332
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:
|
|
Note the priority used to be 6 but it was changed to 9 in ad51973 as part of #1625. I'm not sure exactly why, but @mukeshpanchal27 probably knows. |
|
It came from a review suggestion on that same PR. In #1625 (comment) @joemcgill left a suggestion block containing Commit ad51973 landed about six hours later with that comment verbatim and the number as |
Thanks for tracking down @ekamran - that is helpful. |
Summary
There is no existing issue for this, so reporting it here directly.
auto_sizes_prime_attachment_cachesis registered onthe_contentat priority 9, which is the same priority core uses fordo_blocks. Callbacks that share a priority run in registration order, and core registersdo_blocksindefault-filters.phpwhilewp-settings.phploads, so it is always registered before any plugin. The priming callback therefore runs afterdo_blockson every request, and the cache is never warmed in time to be useful.The inline comment on that line already states the requirement correctly. Only the number disagrees with it.
Relevant technical choices
Priority 8 was requested and agreed during review of #1625, in this thread. The accompanying comment was applied, and the value 9 went in with it. This change restores 8.
At priority 8 the callback registers after core's own callbacks in that bucket, giving this order:
This is the right position. It runs after block hooks have injected markup and after embeds are resolved, so those images are covered as well, and it still runs before
do_blocks. Core itself uses priority 8 for this purpose, with the comment// BEFORE do_blocks().do_blocksis registered at priority 9 in 6.9, 7.0 and 7.1. The plugin requires 6.9 and above.Measured effect
Query counts on a cold cache, single post, full
the_contentchain:Before is
2N + 1for N images. After is flat. Posts with a single image are unchanged, because the function only primes when more than one attachment is found.Rendered output is byte identical before and after at every size tested.
Testing
Full
auto-sizessuite, run before and after the change:PHPCS is clean and PHPStan reports no errors. No test in the repository asserts filter priority or query counts, so no test needed updating.
Use of AI Tools
I used Claude Code on this one. It found the priority mismatch, traced the review history behind it, wrote and ran the measurement scripts and the test matrix above, and drafted this description. The code change itself is a single character. I reviewed the finding, the reasoning and every result before opening this, and I take responsibility for the contribution.