Skip to content

Update transient cache for wp_add_global_styles_for_blocks#6879

Closed
joemcgill wants to merge 12 commits intoWordPress:trunkfrom
joemcgill:update/global-styles-for-blocks-cache
Closed

Update transient cache for wp_add_global_styles_for_blocks#6879
joemcgill wants to merge 12 commits intoWordPress:trunkfrom
joemcgill:update/global-styles-for-blocks-cache

Conversation

@joemcgill
Copy link
Copy Markdown
Member

@joemcgill joemcgill commented Jun 21, 2024

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.

@github-actions
Copy link
Copy Markdown

Test using WordPress Playground

The 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

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@joemcgill joemcgill changed the title DRAFT: Update cache for wp_add_global_styles_for_blocks Update cache for wp_add_global_styles_for_blocks Jun 21, 2024
@joemcgill joemcgill changed the title Update cache for wp_add_global_styles_for_blocks Update transient cache for wp_add_global_styles_for_blocks Jun 21, 2024
Copy link
Copy Markdown
Member

@spacedmonkey spacedmonkey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One question. But what worries me more is that unit tests are currently failing.

Copy link
Copy Markdown
Member Author

@joemcgill joemcgill left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

@spacedmonkey
Copy link
Copy Markdown
Member

@joemcgill On a side now, get_site_transient only result in one database query now. See

Screenshot 2024-06-25 at 22 52 24

Same with get_transient

Screenshot 2024-06-25 at 22 53 38

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.
@joemcgill joemcgill force-pushed the update/global-styles-for-blocks-cache branch from d5fda60 to 3a66386 Compare July 12, 2024 15:40
@joemcgill joemcgill marked this pull request as ready for review July 12, 2024 15:48
@github-actions
Copy link
Copy Markdown

github-actions bot commented Jul 12, 2024

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.

Core Committers: Use this line as a base for the props when committing in SVN:

Props joemcgill, spacedmonkey, costdev, aaronrobertshaw, andrewserong, peterwilsoncc.

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

Co-authored-by: Colin Stewart <79332690+costdev@users.noreply.github.com>
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() ) );
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown

@aaronrobertshaw aaronrobertshaw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

@andrewserong andrewserong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 👍

@joemcgill
Copy link
Copy Markdown
Member Author

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

@joemcgill
Copy link
Copy Markdown
Member Author

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 wp_global_styles post is updated but discovered that the query in WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( wp_get_theme() ) does not return the same post that is created if you first call WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( wp_get_theme(), true ) to create the post. This seems like an unrelated bug so will report it upstream, but wanted to mention in case anyone had any insight into why this might be expected behavior.

Copy link
Copy Markdown
Contributor

@costdev costdev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the work on this @joemcgill! The current tests look good. I've added some standards suggestions in this review.

@joemcgill
Copy link
Copy Markdown
Member Author

Thanks for the review, @costdev. This is ready for another look.

@joemcgill joemcgill requested a review from costdev July 18, 2024 18:07
@joemcgill
Copy link
Copy Markdown
Member Author

I spent some time trying to write a new PHPUnit test that will confirm that the block meta will change whenever the wp_global_styles post is updated but discovered that the query in WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( wp_get_theme() ) does not return the same post that is created if you first call WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( wp_get_theme(), true ) to create the post. This seems like an unrelated bug so will report it upstream, but wanted to mention in case anyone had any insight into why this might be expected behavior.

Gutenberg issue opened, along with a unit test that demonstrates the behavior.

Copy link
Copy Markdown
Contributor

@costdev costdev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 );
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 😅

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@joemcgill
Copy link
Copy Markdown
Member Author

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.

@joemcgill joemcgill self-assigned this Oct 18, 2024
@joemcgill
Copy link
Copy Markdown
Member Author

I've run a set of benchmarks on this locally, which compares 40 runs to the homepage of Twenty Twenty Four.

URL trunk This PR Difference Percentage Difference
Response Time (p10) 70.75 69.21 -1.54 -2.22%
Response Time (p25) 71.51 69.77 -1.74 -2.50%
Response Time (p50) 72.75 70.65 -2.10 -2.97%
Response Time (p75) 74.45 72.05 -2.40 -3.33%
Response Time (p90) 76.19 76.28 0.09 0.12%
wp-before-template (p10) 31.48 31.21 -0.27 -0.86%
wp-before-template (p25) 31.83 31.54 -0.29 -0.92%
wp-before-template (p50) 32.25 32.17 -0.08 -0.25%
wp-before-template (p75) 33.25 33.13 -0.12 -0.36%
wp-before-template (p90) 33.8 34.42 0.62 1.80%
wp-template (p10) 36.39 35.08 -1.31 -3.73%
wp-template (p25) 36.91 35.43 -1.48 -4.18%
wp-template (p50) 37.83 35.96 -1.87 -5.20%
wp-template (p75) 39.14 37.75 -1.39 -3.55%
wp-template (p90) 40.15 39.07 -1.08 -2.69%
wp-total (p10) 68.47 66.85 -1.62 -2.42%
wp-total (p25) 69.49 67.45 -2.04 -3.02%
wp-total (p50) 70.48 68.3 -2.18 -3.19%
wp-total (p75) 71.56 69.64 -1.92 -2.68%
wp-total (p90) 73.91 74.3 0.39 0.53%

@joemcgill
Copy link
Copy Markdown
Member Author

Merged in https://core.trac.wordpress.org/changeset/59256

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

Labels

None yet

Projects

Status: Done 😃

Development

Successfully merging this pull request may close these issues.

6 participants