wordpress-activitypub icon indicating copy to clipboard operation
wordpress-activitypub copied to clipboard

Blocks: only load inline styles when the block is active

Open rghedin opened this issue 1 year ago • 18 comments

Quick summary

After installed, ActivityPub plugin adds a ton of inline CSS on header, in a style tag with the ID activitypub-followers-style-inline-css, even on sites that don't use blocks (as in, use Classic Editor) or FSE.

Maybe there is a code to declare on functions.php to avoid this behavior? Tbh, it seems like a bug, since it shouldn't load in sites without Gutenberg or FSE in the first place.

Bug first raised on issue #14.

Steps to reproduce

  1. Just activate and set the plugin up.
  2. Open the site and check its source code. On header, there will be several lines of inline CSS added with activitypub-followers-style-inline-css ID.

What you expected to happen

That inline code didn't show up in sites that don't use Gutenberg/Blocks or FSE.

What actually happened

CSS loads anyway, no matter the site's configuration.

Impact

Some (< 50%)

Available workarounds?

No but the platform is still usable

Logs or notes

No response

rghedin avatar Dec 11 '23 11:12 rghedin

thanks @rghedin !

pfefferle avatar Dec 11 '23 11:12 pfefferle

I think that the functions.php line that you are looking for is:

function remove_wp_block_library_css(){
  wp_dequeue_style( 'activitypub-followers-style' );
  wp_dequeue_style( 'activitypub-follow-me-style' );
} 
add_action( 'wp_print_styles', 'remove_wp_block_library_css', 100 );

chriskthomas avatar Dec 11 '23 12:12 chriskthomas

Worked like a charm, @writeplace. Thanks!

rghedin avatar Dec 11 '23 13:12 rghedin

Shoot, I thought that the Gutenberg styles would only load like that when the block was active, it appears not. Retitled the issue to indicate what needs fixing.

mattwiebe avatar Dec 14 '23 16:12 mattwiebe

Shoot, I thought that the Gutenberg styles would only load like that when the block was active, it appears not. Retitled the issue to indicate what needs fixing.

Yeah, you'd think, right?

mwt avatar Dec 15 '23 04:12 mwt

I guess there is a residual from Gutenberg being loaded by AP, @mattwiebe:

<link rel="stylesheet" id="wp-components-css" href="https://manualdousuario.net/wp-includes/css/dist/components/style.min.css?ver=6.4.2" media="all"/>

rghedin avatar Dec 16 '23 09:12 rghedin

Any thoughts on this one, @mwt @mattwiebe @writeplace?

rghedin avatar Jan 06 '24 12:01 rghedin

Yeah it shouldn't be loading that either, it's a dependency of the block CSS, but if the block's CSS isn't loaded, it shouldn't be either. This is going to be "fun" to fix :D

mattwiebe avatar Jan 09 '24 17:01 mattwiebe

Fingers crossed 🤞

Meanwhile, do you know any workaround/functions.php to stop it from loading?

rghedin avatar Jan 09 '24 17:01 rghedin

@rghedin I got a bit stumped on this one, the styles were only printing when the block was present for me, but then I realized you are in a classic theme and I was in a block theme! So I tested with a classic theme, and yup, it appears that all block styles get printed on a non-block theme. So I dug in.

_add_default_theme_supports sets a bunch of defaults for block themes, including this line that you will want to try out in your functions.php:

add_filter( 'should_load_separate_core_block_assets', '__return_true' );

It's not really named the best but it's controlling this Since Core behaves this way on purpose, and there's a workaround, I'm going to close this one.

mattwiebe avatar Mar 11 '24 20:03 mattwiebe

Thanks, @mattwiebe! I tried the suggested line in functions.php, but the file didn't go away. This one:

https://domain.com/wp-includes/css/dist/components/style.min.css?ver=6.4.3

☹️

rghedin avatar Mar 12 '24 13:03 rghedin

Is this required by the plugin? have you disabled ActivityPub to see if its gone?

pfefferle avatar Mar 12 '24 13:03 pfefferle

Yeap, @pfefferle. Just disabled ActivityPub in my staging site and that CSS isn't being loaded anymore.

The weirdest part is that Disable Gutenberg, a plugin I use and otherwise disable everything blocks/Gutenberg-related, can't catch this one. I opened an issue over there as well.

rghedin avatar Mar 12 '24 15:03 rghedin

@rghedin I've dug into this more and it seems like a simply unfortunate part of how WP Core manages block assets, not very well outside of Block Themes, it would seem.

If you wish to disable the ActivityPub blocks on your site entirely, and with it this problem, you can also add the following to your functions.php:

add_filter( 'activitypub_site_supports_blocks', '__return_false' );

mattwiebe avatar Mar 12 '24 21:03 mattwiebe

Worked like a charm, @mattwiebe! Thanks :)

rghedin avatar Mar 13 '24 11:03 rghedin

Ouch, false alarm, @mattwiebe. I forgot to activate ActivityPub back, and thought your filter fixed it. In fact, it didn't, as you can see here.

Sorry about that.

rghedin avatar Mar 13 '24 19:03 rghedin

We'll re-open this and see what we can find out

mattwiebe avatar Mar 22 '24 22:03 mattwiebe

I was poking around this issue and noticed that if I load both functions suggested by you guys, it gets rid of that pesky style.min.css:

add_filter( 'should_load_separate_core_block_assets', '__return_true' );
add_filter( 'activitypub_site_supports_blocks', '__return_false' );

rghedin avatar Apr 27 '24 11:04 rghedin