Skip to content
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

Use of uniqid() in generated CSS class names breaks ability to cache parsed CSS #38889

Closed
westonruter opened this issue Feb 17, 2022 · 2 comments · Fixed by #38891 or #39983
Closed

Use of uniqid() in generated CSS class names breaks ability to cache parsed CSS #38889

westonruter opened this issue Feb 17, 2022 · 2 comments · Fixed by #38891 or #39983
Assignees
Labels
CSS Styling Related to editor and front end styles, CSS-specific issues. [Status] In Progress Tracking issues with work in progress [Type] Performance Related to performance efforts

Comments

@westonruter
Copy link
Member

Description

In a support forum topic for the AMP plugin, a user reported their site slowed down after WordPress 5.9 was released. The cause turned out to be that the AMP plugin's caching of parsed CSS automatically disabled since the CSS on the page contained randomness. This disabling is done to prevent filling up the options table with garbage transient data. I found the cause to be the use of uniqid() in functions like wp_render_layout_support_flag():

https://github.com/WordPress/wordpress-develop/blob/8ec1cb83e346853588606b228b8eaa5921d47782/src/wp-includes/block-supports/layout.php#L162-L176

The use of uniqid() actually came up before in the context of the Twenty Seventeen theme. In Core Trac #44883 it was replaced successfully with wp_unique_id().

The result is that instead of a class name like wp-container-620e88a091db4 being generated, where 620e88a091db4 is random with each page load, a class name like wp-container-5 is generated which is stable across page loads.

I created a plugin that replaces uniqid() with wp_unique_id() with successful results: https://gist.github.com/westonruter/71852bfeea22ebd7b570865a95b169d6

Related: #35376

Step-by-step reproduction instructions

Add a group block or columns block, view the frontend, and see random class names generated with each page load.

Example post_content:

<!-- wp:columns -->
<div class="wp-block-columns"><!-- wp:column {"style":{"spacing":{"padding":{"top":"10px","right":"10px","bottom":"10px","left":"10px"}}},"backgroundColor":"white"} -->
<div class="wp-block-column has-white-background-color has-background" style="padding-top:10px;padding-right:10px;padding-bottom:10px;padding-left:10px"><!-- wp:paragraph -->
<p>Col 1</p>
<!-- /wp:paragraph --></div>
<!-- /wp:column -->

<!-- wp:column {"style":{"spacing":{"padding":{"top":"10px","right":"10px","bottom":"10px","left":"10px"}}},"backgroundColor":"black"} -->
<div class="wp-block-column has-black-background-color has-background" style="padding-top:10px;padding-right:10px;padding-bottom:10px;padding-left:10px"><!-- wp:paragraph -->
<p>Col 2</p>
<!-- /wp:paragraph --></div>
<!-- /wp:column --></div>
<!-- /wp:columns -->

<!-- wp:group {"backgroundColor":"black","className":"is-style-twentytwentyone-border"} -->
<div class="wp-block-group is-style-twentytwentyone-border has-black-background-color has-background"><!-- wp:paragraph -->
<p>Group</p>
<!-- /wp:paragraph --></div>
<!-- /wp:group -->

Screenshots, screen recording, code snippet

No response

Environment info

No response

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

@westonruter westonruter added the CSS Styling Related to editor and front end styles, CSS-specific issues. label Feb 17, 2022
@github-actions github-actions bot added the [Status] In Progress Tracking issues with work in progress label Feb 17, 2022
@westonruter westonruter changed the title Use of uniquid() in generated CSS class names breaks ability to cache parsed CSS Use of uniqid() in generated CSS class names breaks ability to cache parsed CSS Feb 17, 2022
@gziolo gziolo added the [Type] Performance Related to performance efforts label Feb 17, 2022
@ramonjd
Copy link
Member

ramonjd commented Feb 17, 2022

It makes it hard to test output against static fixtures as well, which is not as important as the issue raised here of course! :)

Maybe we can look at this as part of the style engine work.

Commented over here as well, but maybe a hash would work better in the interim (?) until we work out a neater solution.

markjaquith pushed a commit to markjaquith/WordPress that referenced this issue Mar 29, 2022
…ass names.

Backports changes from WordPress/gutenberg#38891.
See WordPress/gutenberg#38889.

Props westonruter, mamaduka.
See #55474.

Built from https://develop.svn.wordpress.org/trunk@53012


git-svn-id: http://core.svn.wordpress.org/trunk@52601 1a063a9b-81f0-0310-95a4-ce76da25c4cd
pento pushed a commit to WordPress/wordpress-develop that referenced this issue Mar 29, 2022
…ass names.

Backports changes from WordPress/gutenberg#38891.
See WordPress/gutenberg#38889.

Props westonruter, mamaduka.
Merges [53012] to the 5.9 branch.
See #55474.


git-svn-id: https://develop.svn.wordpress.org/branches/5.9@53013 602fd350-edb4-49c9-b593-d223f7449a82
markjaquith pushed a commit to markjaquith/WordPress that referenced this issue Mar 29, 2022
…ass names.

Backports changes from WordPress/gutenberg#38891.
See WordPress/gutenberg#38889.

Props westonruter, mamaduka.
Merges [53012] to the 5.9 branch.
See #55474.

Built from https://develop.svn.wordpress.org/branches/5.9@53013


git-svn-id: http://core.svn.wordpress.org/branches/5.9@52602 1a063a9b-81f0-0310-95a4-ce76da25c4cd
gMagicScott pushed a commit to gMagicScott/core.wordpress-mirror that referenced this issue Mar 29, 2022
…ass names.

Backports changes from WordPress/gutenberg#38891.
See WordPress/gutenberg#38889.

Props westonruter, mamaduka.
See #55474.

Built from https://develop.svn.wordpress.org/trunk@53012


git-svn-id: https://core.svn.wordpress.org/trunk@52601 1a063a9b-81f0-0310-95a4-ce76da25c4cd
gMagicScott pushed a commit to gMagicScott/core.wordpress-mirror that referenced this issue Mar 29, 2022
…ass names.

Backports changes from WordPress/gutenberg#38891.
See WordPress/gutenberg#38889.

Props westonruter, mamaduka.
Merges [53012] to the 5.9 branch.
See #55474.

Built from https://develop.svn.wordpress.org/branches/5.9@53013


git-svn-id: https://core.svn.wordpress.org/branches/5.9@52602 1a063a9b-81f0-0310-95a4-ce76da25c4cd
@westonruter
Copy link
Member Author

The uniqid() function just ended up in the codebase again, this time in block_core_gallery_render():

Via #38164

@westonruter westonruter reopened this Apr 1, 2022
whereiscodedude pushed a commit to whereiscodedude/wpss that referenced this issue Sep 18, 2022
…ass names.

Backports changes from WordPress/gutenberg#38891.
See WordPress/gutenberg#38889.

Props westonruter, mamaduka.
Merges [53012] to the 5.9 branch.
See #55474.

Built from https://develop.svn.wordpress.org/branches/5.9@53013
VenusPR added a commit to VenusPR/Wordpress_Richard that referenced this issue Mar 9, 2023
…ass names.

Backports changes from WordPress/gutenberg#38891.
See WordPress/gutenberg#38889.

Props westonruter, mamaduka.
Merges [53012] to the 5.9 branch.
See #55474.

Built from https://develop.svn.wordpress.org/branches/5.9@53013


git-svn-id: http://core.svn.wordpress.org/branches/5.9@52602 1a063a9b-81f0-0310-95a4-ce76da25c4cd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CSS Styling Related to editor and front end styles, CSS-specific issues. [Status] In Progress Tracking issues with work in progress [Type] Performance Related to performance efforts
Projects
None yet
3 participants