Update transient cache for wp_add_global_styles_for_blocks#6879
Update transient cache for wp_add_global_styles_for_blocks#6879joemcgill wants to merge 12 commits intoWordPress:trunkfrom
wp_add_global_styles_for_blocks#6879Conversation
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
wp_add_global_styles_for_blockswp_add_global_styles_for_blocks
wp_add_global_styles_for_blockswp_add_global_styles_for_blocks
spacedmonkey
left a comment
There was a problem hiding this comment.
One question. But what worries me more is that unit tests are currently failing.
joemcgill
left a comment
There was a problem hiding this comment.
Thanks @spacedmonkey. The unit test failures are expected currently, because I didn't update the new tests yet. I wanted to get feedback on the approach first, but will update the test before making this change (if at all).
|
@joemcgill On a side now,
Same with
|
This uses a consistent cache key with the cache busting hash saved to the cache value rather than as part of the key. This allows us to remove the expiry time for the cache and only invalidate when the hash changes. This also swaps the use of a site transient to the use of a regular transient so that the option will be autoloaded since there is no expiration time.
d5fda60 to
3a66386
Compare
|
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 Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
| if ( ! is_array( $cached ) ) { | ||
| $cached = array(); | ||
| // Hash the merged WP_Theme_JSON data to bust cache on settings or styles change. | ||
| $cache_hash = md5( wp_json_encode( $tree->get_raw_data() ) ); |
There was a problem hiding this comment.
This is a much simpler hash generation, and accounts for a much broader set of changes that might happen to the merged Theme JSON data, which could affect global block styles, which is less risky.
aaronrobertshaw
left a comment
There was a problem hiding this comment.
This has been updated to also fix a bug that was reported in https://core.trac.wordpress.org/ticket/59595#comment:71 with testing instructions.
Thanks @joemcgill for updating this PR with a fix for the reported issue 👍
After applying the changes here, the global block styles update as expected on the frontend.
| Before Revert (ad135d5) | After |
|---|---|
Screen.Recording.2024-07-15.at.10.17.40.AM.mp4 |
Screen.Recording.2024-07-15.at.10.15.54.AM.mp4 |
It looks like this PR needs a rebase but in general it's looking pretty close to me.
andrewserong
left a comment
There was a problem hiding this comment.
but in general it's looking pretty close to me.
Same here, I like how this makes for a simpler and broader hash, it sounds like this will be a fair bit safer 👍
|
Thanks all, I'll update this PR so it applies cleanly after the revert of the original caching so we can try again for 6.6.1 |
|
I've refreshed this PR against trunk, which adds back the caching that was reverted in https://core.trac.wordpress.org/changeset/58710 using the updated approach. I spent some time trying to write a new PHPUnit test that will confirm that the block meta will change whenever the |
costdev
left a comment
There was a problem hiding this comment.
Thanks for the work on this @joemcgill! The current tests look good. I've added some standards suggestions in this review.
Co-authored-by: Colin Stewart <79332690+costdev@users.noreply.github.com>
|
Thanks for the review, @costdev. This is ready for another look. |
Gutenberg issue opened, along with a unit test that demonstrates the behavior. |
costdev
left a comment
There was a problem hiding this comment.
Thanks for the updates @joemcgill! Looks good to me 👍
| if ( isset( $cached['blocks'][ $cache_node_key ] ) ) { | ||
| $block_css = $cached['blocks'][ $cache_node_key ]; | ||
| } else { | ||
| $block_css = $tree->get_styles_for_block( $metadata ); |
There was a problem hiding this comment.
Just to make sure, can the meta data include block settings in the editor that will effect the output styles?
If so, then this may have incorrect data when a block is updated. If not then it's dandy.
There was a problem hiding this comment.
I'm not sure I fully grasp the question, sorry.
Global styles for blocks set in the editor are saved to the Global Styles CPT, which are retrieved and merged with base and theme data via WP_Theme_JSON_Resolver::get_merged_data(); above. While in the editor, if global styles for a block type are adjusted without saving, there's a JS hook that outputs updated styles overriding those enqueued here.
My understanding is that the styles generated here only need to reflect what is saved for Global Styles.
Not sure if that helps, answers the question, or just confuses things 😅
There was a problem hiding this comment.
My understanding is that the styles generated here only need to reflect what is saved for Global Styles.
That helps and was what I was asking, thanks Aaron.
|
I've refreshed this PR and added an additional unit test to confirm that the cache gets invalidated whenever the global-styles post is updated in the editor. |
|
I've run a set of benchmarks on this locally, which compares 40 runs to the homepage of Twenty Twenty Four.
|


This uses a consistent cache key with the cache busting hash saved to the cache value rather than as part of the key. This allows us to remove the expiry time for the cache and only invalidate when the hash changes.
This also swaps the use of a site transient to the use of a regular transient so that the option will be autoloaded since there is no expiration time.
Trac ticket: https://core.trac.wordpress.org/ticket/61679
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.