Skip to content

Enhanced Responsive Images: Fix cache priming running after block rendering - #2641

Open
ekamran wants to merge 2 commits into
WordPress:trunkfrom
ekamran:fix/auto-sizes-prime-cache-priority
Open

Enhanced Responsive Images: Fix cache priming running after block rendering#2641
ekamran wants to merge 2 commits into
WordPress:trunkfrom
ekamran:fix/auto-sizes-prime-cache-priority

Conversation

@ekamran

@ekamran ekamran commented Aug 25, 2026

Copy link
Copy Markdown

Summary

There is no existing issue for this, so reporting it here directly.

auto_sizes_prime_attachment_caches is registered on the_content at priority 9, which is the same priority core uses for do_blocks. Callbacks that share 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 on 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:

[8] apply_block_hooks_to_content_from_post_object
[8] WP_Embed::run_shortcode
[8] WP_Embed::autoembed
[8] auto_sizes_prime_attachment_caches
[9] do_blocks

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_blocks is 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_content chain:

Images Before After
1 3 3
2 5 3
5 11 3
10 21 3
20 41 3

Before is 2N + 1 for 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-sizes suite, run before and after the change:

Core Mode Result
6.9.4 single site 169 tests, 185 assertions, identical before and after
6.9.4 multisite 169 tests, 185 assertions, identical before and after
7.2-alpha single site 169 tests, 185 assertions, identical before and after
7.2-alpha multisite 169 tests, 185 assertions, identical before and after

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.

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.
@github-actions

github-actions Bot commented Aug 25, 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>
Co-authored-by: westonruter <westonruter@git.wordpress.org>
Co-authored-by: adamsilverstein <adamsilverstein@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 Issues for the Enhanced Responsive Images plugin (formerly Auto Sizes) label Aug 25, 2026
@codecov

codecov Bot commented Aug 25, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 70.35%. Comparing base (77ab464) to head (8f72b07).

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           
Flag Coverage Δ
multisite 70.35% <ø> (ø)
single 35.17% <ø> (ø)

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.

@westonruter westonruter added the [Type] Bug An existing feature is broken label Aug 26, 2026
@westonruter westonruter added this to the auto-sizes n.e.x.t milestone Aug 26, 2026
@westonruter

Copy link
Copy Markdown
Member

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.

@ekamran

ekamran commented Aug 26, 2026

Copy link
Copy Markdown
Author

It came from a review suggestion on that same PR. In #1625 (comment) @joemcgill left a suggestion block containing 8, together with the comment line that is in the file today, and wrote "Registering this hook on priority 8 still works as expected."

Commit ad51973 landed about six hours later with that comment verbatim and the number as 9. Nothing anywhere in #1625 proposes 9, so it looks like the suggestion was applied by hand rather than with the Commit suggestion button.

@westonruter

@adamsilverstein

Copy link
Copy Markdown
Member

It came from a review suggestion on that same PR. In #1625 (comment) @joemcgill left a suggestion block containing 8, together with the comment line that is in the file today, and wrote "Registering this hook on priority 8 still works as expected."

Thanks for tracking down @ekamran - that is helpful.

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

Labels

[Plugin] Enhanced Responsive Images Issues for the Enhanced Responsive Images plugin (formerly Auto Sizes) [Type] Bug An existing feature is broken

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants