Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update transient cache for wp_add_global_styles_for_blocks #6879

Open
wants to merge 4 commits into
base: trunk
Choose a base branch
from

Conversation

joemcgill
Copy link
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.

Update July 12, 2024
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.

Trac ticket: https://core.trac.wordpress.org/ticket/59595


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.

Copy link

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 Jun 21, 2024
@joemcgill joemcgill changed the title Update cache for wp_add_global_styles_for_blocks Jun 21, 2024
Copy link
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.

if ( ! is_array( $cached ) ) {
$cached = array();
$cache_key = 'wp_styles_for_blocks';
$cached = get_transient( $cache_key );
Copy link
Member

Choose a reason for hiding this comment

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

Why is this changed to get_transient from get_site_transient?

Copy link
Member Author

Choose a reason for hiding this comment

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

This cache needs to be site specific, because it uses global settings and the computed styles for blocks in the hash, which incorporates users settings. When the hash key is part of the cache key name, every site on a multisite network with different settings will generate unique keys, which will expire naturally in 10 minutes.

To avoid the extra DB lookup for a transient with an expiration, I'm looking at making the cache key consistent, and put the hash inside the cached data for invalidation, but that means we need to save separate transients for each site on a multisite install, rather than as a shared transient for the whole network.

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

if ( ! is_array( $cached ) ) {
$cached = array();
$cache_key = 'wp_styles_for_blocks';
$cached = get_transient( $cache_key );
Copy link
Member Author

Choose a reason for hiding this comment

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

This cache needs to be site specific, because it uses global settings and the computed styles for blocks in the hash, which incorporates users settings. When the hash key is part of the cache key name, every site on a multisite network with different settings will generate unique keys, which will expire naturally in 10 minutes.

To avoid the extra DB lookup for a transient with an expiration, I'm looking at making the cache key consistent, and put the hash inside the cached data for invalidation, but that means we need to save separate transients for each site on a multisite install, rather than as a shared transient for the whole network.

@spacedmonkey
Copy link
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
Copy link

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.

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

@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

@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
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
5 participants