Skip to content

Block Supports: Prevent Additional CSS duplication inside Query Loop#11859

Open
Mustafabharmal wants to merge 1 commit into
WordPress:trunkfrom
Mustafabharmal:fix/65268-prevent-additional-css-duplication-query-loop
Open

Block Supports: Prevent Additional CSS duplication inside Query Loop#11859
Mustafabharmal wants to merge 1 commit into
WordPress:trunkfrom
Mustafabharmal:fix/65268-prevent-additional-css-duplication-query-loop

Conversation

@Mustafabharmal
Copy link
Copy Markdown

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

What

When a block inside a Query Loop has Additional CSS, wp_add_inline_style was called once per post in the loop, duplicating the same CSS N times.

Why

wp_render_custom_css_support_styles hooks render_block_data, which fires for every block render. wp_unique_id_from_values produces 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_names array. The first time a class name is seen the CSS is enqueued; subsequent renders skip it.

Gutenberg PR: WordPress/gutenberg#78282

@github-actions
Copy link
Copy Markdown

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

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

@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

  • 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

@mukeshpanchal27 mukeshpanchal27 left a comment

Choose a reason for hiding this comment

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

Can you add unit tests that verify the changes?

Comment on lines +60 to +63
* 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).
*/
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
* 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).
*/

Comment on lines +68 to +72
/*
* 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.
*/
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
/*
* 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.
*/

Comment on lines +60 to +75
* 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 );
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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:

Suggested change
* 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 );
}

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants