-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Theme JSON and Resolver: Update block caching logic to clear based on number of registered blocks #3373
Conversation
… number of registered blocks
This is working as advertised for me, thanks @andrewserong ! It seems like a good approach, assuming that the number of blocks alone is enough to determine the validity of any cached theme data. I guess if it turns out that it's not down the road, I'll leave to folks with more context to determine the robustness of the approach, but it LGTM! |
I was wondering about the robustness of this too. This works fine in core without any plugins, but the way this would break is if someone unregisters a block and registers a new one after I don't expect that someone would unregister and re-register a block with the same name as often, so maybe something like this could work? hash( 'crc32', implode( array_keys( $blocks ) ) ) |
It looks like we're already potentially doing this in |
If we make sure that |
@andrewserong do you think this fix might solve WordPress/gutenberg#44619? It seems related somehow to WordPress/gutenberg#44434. |
Thanks for the ping @ndiego, yes, it should fix that issue, too, as I believe it's the same underlying problem. |
Thanks for the feedback @ajlende! That's a good point about registration / re-registration of blocks. I think ideally we wouldn't need to depend on the Template Part block being registered in a particular order, but I'm also all in favour of pragmatic fixes to resolve the problem in the shorter-term 🙂 I'll have a play with either pulling in your changes to this PR, or updating the caching logic today and see what I can come up with! |
Great points about the issues of robustness @ajlende and @ramonjd! I've had a go at opening up (yet another) alternative PR in #3392 that proposes to add a One concern I have with the idea in #3384 is that altering the order in which blocks are registered also seems to be a bit brittle, in that we then need to be careful about future changes to the block registration order, and it's still possible for a plugin to accidentally break things if something similar to what the Post Template block does, happens in a 3rd party block. Happy to keep exploring options tomorrow, if anyone has any extra feedback. Thanks so much for digging in, too, @ajlende! 🙇 |
* This ensures that Theme JSON data accessed at registration time | ||
* does not result in stale block data. | ||
*/ | ||
return 'registered-blocks-' . count( $blocks ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this result in a case where we end up with the same number of blocks but they're different? Perhaps we could get a hash of the $blocks
array instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that seems to be a concern shared by some folks: #3373 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately we can't rely on only the registered block names because there is at least one instance where we unregister an old version of a block and re-register a new one with the same name within the same function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately we can't rely on only the registered block names because there is at least one instance where we unregister an old version of a block and re-register a new one with the same name within the same function.
Thank you for pointing that out! I just tried to address that at #3359 (comment).
Thanks folks! Now that #3359 has landed I'll close this out. |
Ensure that block data does not become stale in the Theme JSON and Theme JSON Resolver classes if they are called during block registration. Specifically, attempt to resolve the issue described in: WordPress/gutenberg#44434
This is an alternative to #3352 and #3359. The goal in this PR is to take a similar general approach to #3359, but focus on clearing the cached data when the number of registered blocks changes. This appears to have the same desired result of fresh data once all blocks have been registered, without removing the caching logic altogether.
Scope of changes:
$theme_cache_key
andget_new_theme_cache_key
toWP_Theme_JSON_Resolver
to allow cache busting when the list of registered blocks changes.$blocks_cache_key
andget_new_blocks_cache_key
toWP_Theme_JSON
, also to allow cache busting when the list of registered blocks changes.Trac ticket: https://core.trac.wordpress.org/ticket/56644
Testing this change:
0
This screenshot shows the Site Title block with custom background color and padding styling, and the Buttons block using the theme's styling:
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.