-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Ensure that post thumbnail is cached in post template block. #40572
Conversation
@@ -39,6 +39,7 @@ function render_block_core_post_template( $attributes, $content, $block ) { | |||
if ( ! $query->have_posts() ) { | |||
return ''; | |||
} | |||
update_post_thumbnail_cache( $query ); |
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.
Since a Query Loop block might not contain the Post Featured Image
block, wouldn't this result in extra db queries in that case? Can we incorporate this in the Post Featured Image block somehow and have some performance improvements?
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.
Fair point. I have moved the logic to the feature image block.
I tried to get the query as part of context, why I am not sure if this is even possible. My worry is the the feature image is used in a query block or other place, where the query is different than the main query on the page.
Any ideas @ntsekouras
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.
👋 Post Featured Image knows if is in a Query Loop if the queryId
prop is filled from its context. An example would be here, but we would need to check for queryId
. Noting that the queryId could be zero.
I'm not really familiar with this optimization, but would it make sense in the block itself or is it more about passing a query?
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.
The function update_post_thumbnail_cache
accepts the query object as a param. When it does, all thumbnail caches are primed in one request. It uses the WP_Query object to get the posts and then loop the thumbnails.
I think we should pass the query in here, as a feature image block could be used in different query loops.
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.
I took a closer look here and I think this could work well in Post Template block.
foreach ( $block->parsed_block['innerBlocks'] as $inner_block ) {
if ( 'core/post-featured-image' === $inner_block['blockName'] ) {
update_post_thumbnail_cache( $query );
break;
}
}
Can you think of any implications with this code @gziolo?
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.
@ntsekouras, that would be similar to what @spacedmonkey proposes in #40752 for the Navigation block.
Anyway, it largely depends on whether the Post Featured Image is the immediate child in the Post Template or it can be included at any level of nesting. I'm not sure whether $block->parsed_block['innerBlocks']
is a flat list of inner blocks, but it would have to be further investigated if you plan to go this route.
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.
Anyway, it largely depends on whether the Post Featured Image is the immediate child in the Post Template or it can be included at any level of nesting.
Oh.. that's true. It would make sense to have some metrics about this optimization whether it's better to recurse through innerBlocks to find if featured image exists in comparison to the two requests.
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.
It would not be 2 requests, if you have 10 feature images on a page, it this could result in 10 times 2 ( one for post one for meta ) for each thumbnail. I have code in here to recursively loop through blocks to find nested navigation link blocks. I can confirm, that the inner is, is not a full list. The inner block, have inner blocks. So you have to recursively loop through blocks. It is a pain, but I have hidden the logic, so we could adapt it here.
The check here would be much simpler, as we just been a boolean of has feature image block.
@ntsekouras @gziolo Should I move the logic back to the post template block with a recursively lookup for featured-image 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.
It is also worthing noting, I dont have an issue with having this function in both the feature image and post template block. I don't think it hurts having it in both places.
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.
@ntsekouras @gziolo Should I move the logic back to the post template block with a recursively lookup for featured-image blocks?
Yes, I think it's better to be there and pass the specific query. Also without some metrics I'm not even sure if it worths it to recurse through innerBlocks or just call the function 😄
@felixarntz @hellofromtonya Unit test added here too. |
@draganescu This PR maybe helpful for #39658. We need to ensure that thumbnail is primed in cache so that there is a performance regression here. |
a80a58a
to
27b625f
Compare
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.
Besides the nit comments, this LGTM. Thanks @spacedmonkey !
Excuse my WP cache noobness :D but I wonder what is the improvement we gain? Is this going to save a few db calls for a few editors open that happen to have blocks which query for featured image? If there is no persistence all different editors will benefit little. Same applies to the optimisation in #40752 for the Navigation block.
How can we test there is any performance gain? Later edit: right after submitting I realised I was missing the point :D these are rendering optimisations. In this case for the featured image, it's similar to how However the cover block calls |
@draganescu I use a plugin called query monitor to check for the performance of the page. Here are the results. This is just on a page with 3 featured images. The improvement scales, do it primes all thumbnails at once. Priming them all at once, as a number of benefits, including few queries and better performance persistent object cache is enabled.
See #40853 for more context. Calling this function multiple times is it not a problem, so I go out of my way to call it there as well. This PR pairs with #40853. But fixes have to done in both places, as cover blocks maybe calls outside of post template block context. |
@ntsekouras Feedback actioned. |
Let's go ahead and merge @spacedmonkey . With the change in the cover block and the potentially compounding improvement there is no downside only upside 👍🏻 |
Some notes on this:
This function tries to find one block that uses the featured image, then returns immediately. Suggest this instead:
Suggest this:
if ( 'core/cover' === $block->name && $block->attributes && isset( $block->attributes['useFeaturedImage'] ) && $block->attributes['useFeaturedImage'] ) { can be shortened to: if ( 'core/cover' === $block->name && ! empty( $block->attributes['useFeaturedImage'] ) ) { |
@costdev made a PR with your suggestions. |
I cherry-picked this to wp/6.0 branch to be included in WordPress 6.0 RC2 later today 482d1b1c54 |
* Ensure that post thumbnail is cached in post template block. * Check for inner blocks that use featured images. * Default value. * Improve logic.
What?
Ensure that post thumbnails are correctly primed when using the post template block.
Related to #40571.
Why?
If this function is not called, it results in one query to post meta table. Calling
update_post_thumbnail_cache
primes all caches in one.How?
Testing Instructions
Screenshots or screencast