Block Supports: Prevent Additional CSS duplication inside Query Loop#11859
Conversation
|
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 Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe 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
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
mukeshpanchal27
left a comment
There was a problem hiding this comment.
Can you add unit tests that verify the changes?
| * Track which class names have already had their CSS enqueued to prevent | ||
| * duplicate styles when the same block is rendered multiple times inside | ||
| * a Query Loop (render_block_data fires once per loop iteration). | ||
| */ |
There was a problem hiding this comment.
| * Track which class names have already had their CSS enqueued to prevent | |
| * duplicate styles when the same block is rendered multiple times inside | |
| * a Query Loop (render_block_data fires once per loop iteration). | |
| */ | |
| * Track which class names have already had their CSS enqueued to prevent | |
| * duplicate styles when the same block is rendered multiple times inside | |
| * a Query Loop (render_block_data fires once per loop iteration). | |
| */ |
| /* | ||
| * Register and add inline style for block custom CSS. | ||
| * The style depends on global-styles to ensure custom CSS loads after | ||
| * and can override global styles. | ||
| */ |
There was a problem hiding this comment.
| /* | |
| * Register and add inline style for block custom CSS. | |
| * The style depends on global-styles to ensure custom CSS loads after | |
| * and can override global styles. | |
| */ | |
| /* | |
| * Register and add inline style for block custom CSS. | |
| * The style depends on global-styles to ensure custom CSS loads after | |
| * and can override global styles. | |
| */ |
| * Track which class names have already had their CSS enqueued to prevent | ||
| * duplicate styles when the same block is rendered multiple times inside | ||
| * a Query Loop (render_block_data fires once per loop iteration). | ||
| */ | ||
| static $enqueued_class_names = array(); | ||
|
|
||
| if ( ! isset( $enqueued_class_names[ $class_name ] ) ) { | ||
| $enqueued_class_names[ $class_name ] = true; | ||
| /* | ||
| * Register and add inline style for block custom CSS. | ||
| * The style depends on global-styles to ensure custom CSS loads after | ||
| * and can override global styles. | ||
| */ | ||
| wp_register_style( 'wp-block-custom-css', false, array( 'global-styles' ) ); | ||
| wp_add_inline_style( 'wp-block-custom-css', $processed_css ); | ||
| } |
There was a problem hiding this comment.
A static variable can be very problematic for unit tests. I don't recommend using it. I suggest instead that we look at what wp_add_inline_style() is doing under the hood. It calls WP_Styles::add_inline_style( $handle, $code ) which in turn adds the provided code to an array of existing inline styles and then passes that amended array to WP_Styles::add_data( $handle, 'after', $after ).
So I think a better approach would be to use the underlying WP_Styles::$extra array instead of a static $enqueued_class_names here:
| * Track which class names have already had their CSS enqueued to prevent | |
| * duplicate styles when the same block is rendered multiple times inside | |
| * a Query Loop (render_block_data fires once per loop iteration). | |
| */ | |
| static $enqueued_class_names = array(); | |
| if ( ! isset( $enqueued_class_names[ $class_name ] ) ) { | |
| $enqueued_class_names[ $class_name ] = true; | |
| /* | |
| * Register and add inline style for block custom CSS. | |
| * The style depends on global-styles to ensure custom CSS loads after | |
| * and can override global styles. | |
| */ | |
| wp_register_style( 'wp-block-custom-css', false, array( 'global-styles' ) ); | |
| wp_add_inline_style( 'wp-block-custom-css', $processed_css ); | |
| } | |
| * Track which class names have already had their CSS enqueued to prevent | |
| * duplicate styles when the same block is rendered multiple times inside | |
| * a Query Loop (render_block_data fires once per loop iteration). | |
| */ | |
| $handle = 'wp-block-custom-css'; | |
| if ( ! wp_style_is( $handle, 'registered' ) ) { | |
| wp_register_style( $handle, false, array( 'global-styles' ) ); | |
| } | |
| $after_styles = wp_styles()->get_data( $handle, 'after' ); | |
| if ( ! is_array( $after_styles ) ) { | |
| $after_styles = array(); | |
| } | |
| if ( ! in_array( $processed_css, $after_styles, true ) ) { | |
| wp_add_inline_style( $handle, $processed_css ); | |
| } |
Trac ticket: https://core.trac.wordpress.org/ticket/65268
What
When a block inside a Query Loop has Additional CSS,
wp_add_inline_stylewas called once per post in the loop, duplicating the same CSS N times.Why
wp_render_custom_css_support_styleshooksrender_block_data, which fires for every block render.wp_unique_id_from_valuesproduces the same hash for the same block template data on every loop iteration, so the same CSS gets injected repeatedly.How
Added a
static $enqueued_class_namesarray. The first time a class name is seen the CSS is enqueued; subsequent renders skip it.Gutenberg PR: WordPress/gutenberg#78282