Block Supports: Prevent Additional CSS duplication inside Query Loop#11859
Block Supports: Prevent Additional CSS duplication inside Query Loop#11859Mustafabharmal wants to merge 12 commits into
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?
|
We should add a test for this as well. |
| * 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). | |
| * Skip CSS that has already been added. Blocks with identical attributes | |
| * share the same class name and processed CSS via wp_unique_id_from_values(), | |
| * so the same style would otherwise be enqueued more than once (e.g. inside | |
| * a Query Loop or when blocks share identical custom CSS). |
Since this issue is not limited to the Query Loop block, I recommend updating this comments to be more accurate.
For example, create content like the following:
<!-- wp:paragraph {"style":{"css":"background: red;"}} -->
<p class="has-custom-css">Hello World</p>
<!-- /wp:paragraph -->
<!-- wp:paragraph {"style":{"css":"background: red;"}} -->
<p class="has-custom-css">Hello World</p>
<!-- /wp:paragraph -->
<!-- wp:paragraph {"style":{"css":"background: red;"}} -->
<p class="has-custom-css">Hello World</p>
<!-- /wp:paragraph -->
<!-- wp:paragraph {"style":{"css":"background: red;"}} -->
<p class="has-custom-css">Hello World</p>
<!-- /wp:paragraph -->This PR will eliminate the duplicate CSS.
Before:
<style id="wp-block-custom-css-inline-css">
:root :where(.wp-custom-css-38db0572){background: red;}
:root :where(.wp-custom-css-38db0572){background: red;}
:root :where(.wp-custom-css-38db0572){background: red;}
:root :where(.wp-custom-css-38db0572){background: red;}
</style>After:
<style id="wp-block-custom-css-inline-css">
:root :where(.wp-custom-css-38db0572){background: red;}
</style>There was a problem hiding this comment.
Good call, although I suggest {@see wp_unique_id_from_values()} to link the reference.
|
@Mustafabharmal Could you add some unit tests for this PR please? After the commit we can check as well if the failing check is working again. We'd like to get this a step further to potentially include it in WP 7.0.1 Thanks for your help here! |
t-hamano
left a comment
There was a problem hiding this comment.
LGTM! I would like to commit this later.
| wp_add_inline_style( 'wp-block-custom-css', $processed_css ); | ||
| $handle = 'wp-block-custom-css'; | ||
| if ( ! wp_style_is( $handle, 'registered' ) ) { | ||
| wp_register_style( $handle, false, array( 'global-styles' ) ); |
There was a problem hiding this comment.
I have a slight concern about this code. What happens if a consumer intentionally deregister global-styles? In that case, global-styles would be forcibly registereded only when a block with custom CSS exists on the page, which might go against the consumer's intent. Shouldn't custom CSS operate independently of global styles?
However, since this logic was not introduced in this PR, it is not a blocker. We may be able to consider a fix in a follow-up.
Go for it. |
Unblocking request changes as unit tests have been added.
|
@pento, it appears that unnecessary changes were added due to the force push. |
+1 |
| "ext-ftp": "*", | ||
| "ext-mysqli": "*", | ||
| "ext-ssh2": "*" |
There was a problem hiding this comment.
Something seems to have gone wrong in the repository. It looks like all open PRs were force-pushed by panto.
There may be an issue with the automation or repository configuration. Is anyone checking that?
There was a problem hiding this comment.
| /** | ||
| * Skip CSS that has already been added. Blocks with identical attributes | ||
| * share the same class name and processed CSS via {@see wp_unique_id_from_values()}, | ||
| * so the same style would otherwise be enqueued more than once (e.g. inside | ||
| * a Query Loop or when blocks share identical custom CSS). | ||
| */ |
There was a problem hiding this comment.
| /** | |
| * Skip CSS that has already been added. Blocks with identical attributes | |
| * share the same class name and processed CSS via {@see wp_unique_id_from_values()}, | |
| * so the same style would otherwise be enqueued more than once (e.g. inside | |
| * a Query Loop or when blocks share identical custom CSS). | |
| */ | |
| /* | |
| * Skip CSS that has already been added. Blocks with identical attributes | |
| * share the same class name and processed CSS via {@see wp_unique_id_from_values()}, | |
| * so the same style would otherwise be enqueued more than once (e.g. inside | |
| * a Query Loop or when blocks share identical custom CSS). | |
| */ |
Nit - Use Multi-line comments https://developer.wordpress.org/coding-standards/inline-documentation-standards/php/#5-2-multi-line-comments
There was a problem hiding this comment.
The docblock style was intentional so that the inline @see tag is parsed.
There was a problem hiding this comment.
Oh, I see. Let's put it back
Co-authored-by: t-hamano <wildworks@git.wordpress.org>
…yle at end of test
…gnment In addition to being simpler, this ensures PHPStan is able to statically-analyze the return value.
8aabf1e to
aa8abc4
Compare
|
I'm not entirely sure of the cause, but when I checked out the latest trunk locally and rebased this branch on top of it, the unintended changes seem to have disappeared. |
Co-authored-by: Mukesh Panchal <mukeshpanchal27@users.noreply.github.com>
| * 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.
| /** | |
| /* |
Multi-line comment
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