Skip to content

Global Styles: Invalidate global block styles cache on style change#7017

Closed
aaronrobertshaw wants to merge 1 commit intoWordPress:trunkfrom
aaronrobertshaw:fix/caching-of-global-block-styles
Closed

Global Styles: Invalidate global block styles cache on style change#7017
aaronrobertshaw wants to merge 1 commit intoWordPress:trunkfrom
aaronrobertshaw:fix/caching-of-global-block-styles

Conversation

@aaronrobertshaw
Copy link
Copy Markdown

@aaronrobertshaw aaronrobertshaw commented Jul 12, 2024

This PR is an initial pass at preventing stale global block styles from being rendered on the frontend in 6.6

Problem

Caching was recently added to the global block styles in wp_add_global_styles_for_blocks, this was keyed only on the block name or metadata meaning the cached styles were returned even if the styles had recently changed.

Proposed Solution

Cache the global block styles using a hash of the style data as well.

Steps to replicate issue on trunk

Copied from #71

  1. Open the site editor on a WP 6.6 RC3 site running Twenty Twenty Four theme
  2. Click the editor preview to go into edit mode
  3. Open the Global Styles sidebar panel on the right hand side
  4. Click Blocks
  5. Select the Button block
  6. Give the Button block a style, e.g. Background color set to a gradient
  7. Save the global styles
  8. Load the site frontend — notice that the styling on the Button is applied
  9. Go back to global styles and give the Button block another style, e.g. large font size
  10. Save the global styles
  11. Load the site frontend and notice that the Button block's styling has not changed — it'll be cached with the previous styles that were originally saved

Test Instructions

  1. Checkout this PR and repeat the steps above. This time the styles should update appropriately
  2. Confirm that the cache is still used when styles haven't changed.

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

Props go to @tellthemachines for isolating the cause and @andrewserong for his assistance in debugging also.


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

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 aaronrobertshaw, isabel_brison.

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

@aaronrobertshaw
Copy link
Copy Markdown
Author

I'll be AFK for a couple of days. So please feel free to push changes, rework, or discard this PR. It's only meant to help find a possible solution for 6.6.

@tellthemachines
Copy link
Copy Markdown
Contributor

I gave this a quick test run on my local and this does seem to fix the issue!

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

Copy link
Copy Markdown
Member

@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, @aaronrobertshaw! This indeed fixes the bug, but splits the responsibility of cache busting in two separate places—the hash added to the transient name, and also to the block node keys stored in the cache. I'd prefer that the responsibility for cache invalidation be handled in one place.

I've proposed an alternative in #6879 that I think will fix this as well, which I'd appreciate feedback on.

@aaronrobertshaw
Copy link
Copy Markdown
Author

Closing in favour of #6879

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants