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

Inline CSS is output twice when rendering uncached version #1

Open
kadamwhite opened this issue Dec 17, 2024 · 0 comments
Open

Inline CSS is output twice when rendering uncached version #1

kadamwhite opened this issue Dec 17, 2024 · 0 comments

Comments

@kadamwhite
Copy link
Collaborator

kadamwhite commented Dec 17, 2024

This is benign beyond very minimal page size and CSS parsing speed efficiency because repeated CSS will simply override itself, but the logic currently causes inline CSS to be output twice.

  • Uncached content is rendered
  • Scripts and styles added by the content rendering are stored
  • We run wp_enqueue_stored_styles() to identify what inline styles will need to be added
  • The inline styles changes are cached alongside the dependencies injected by the content rendering

Because at this point we merge the cached/render-generated styles back into the global store — inclusive of the inline CSS, which will then get computed again during this uncached pageview, in the footer — any such styles end up being added to ->registered['core-block-supports]->extra['after'] twice.

It's not clear to me at this stage whether there's a seam that would let us smartly deduplicate these things, since we don't probably want to do anything like "iterate over all styles after running wp_enqueue_stored_styles so we can then dedupe repeated lines"...

add_action( 'wp_footer', function() {
    foreach ( wp_styles()->registered as $dependency ) {
        if ( ! empty( $dependency->extra['after'] ) ) {
            $dependency->extra['after'] = array_unique( $dependency->extra['after'] );
        }
    }
}, 11 );

...However, maybe the iteration would be minimally impactful and there's a reason that this would be preferable to rendering duplicate CSS rules onto the page...? I guess we at least have a list of impacted styles so we could limit the iteration to only those.

Input welcomed.

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

No branches or pull requests

1 participant