ahmadawais / create-guten-block

📦 A zero-configuration #0CJS developer toolkit for building WordPress Gutenberg block plugins.
https://Awais.dev/cgb-post
MIT License
3.15k stars 327 forks source link

Unnecessary CSS loaded in frontend #210

Closed Azragh closed 4 years ago

Azragh commented 5 years ago

Bug Report

Expected Behavior

After creating / enabling my first guten-block plugin I inspected the pages source code to see at which position my frontend stylesheet for the new block gets included. To my surprise there wasn't just one new file but 6 instead.

Actual Behavior

The other 5 stylesheets are for the editor only (at least I think so? correct me if I'm wrong ;)), so they shouldn't be there at all.

Head before enabling the plugin: image

Head after enabling: image

Steps to Reproduce

  1. Create a block
  2. Look at frontend source
  3. Enable Plugin / Refresh page
  4. Compare =)

I've also tested this on a new WP installation with default theme and no plugins active.

ahmadawais commented 5 years ago

You must have included these CSS by yourself. Coz I don't recognize these new files.

Azragh commented 5 years ago

Nope. As I said after noticing this I've set up a new blank installation with it's default theme and no other plugins active, just to be sure that it's not my or another plugin's fault. I didn't change anything except the name of the new block.

Azragh commented 5 years ago

Just to prove:

Source screenshot of the absolutely blank installation with the twentynineteen theme: image

Process creating the block: image

image

image

So everything fine, nothing changed. There we are: image

After activating, checking the source code again: image

Doesn't matter if I type 'npm start', 'run build' or even eject, or if it's running or not, so what's different at my setup? Could it be because I'm using XAMPP or any related issue?

ahmadawais commented 5 years ago

These are the three files that create-guten-block enqueues https://github.com/ahmadawais/create-guten-block/blob/master/packages/cgb-scripts/template/src/init.php#L32-L54

Everything else comes from your own setup.

Take time to read the links of these scripts and their names.

Your theme is putting out these links.

Press COMMAND (⌘) + F and search plugins/testblock only these scripts are through create-guten-block. Rest are from WordPress, or your theme, or you are looking at the source while logged in instead of looking at Incognito mode for what actually gets loaded for the user.

Peace! ✌️

Azragh commented 5 years ago

I know that these are the only files called by the plugin (first thing I did was looking at init.php), but the other ones get loaded as soon I enable it. I disable it and their gone - so in some way the plugin has an effect on wordpress' register/enqueue functions - doesn't matter if I'm logged in, inkognito mode or whatever..

And as I said (twice): the theme is twentynineteen - nothing changed.

Anyway, thanks.

Connum commented 4 years ago

For anyone stumbling upon this:

I found the same issue and investigated it. Turns out, the reason for the additional styles being loaded in the frontend is the dependency to wp-editor defined in wp_register_style() of the frontend CSS.

I fixed it by replacing array('wp-editor') with is_admin() ? array('wp-editor') : null.

In my opinion this should be reopened and addressed, and I find it curious that the author didn't catch that when presented with this issue.

ahmadawais commented 4 years ago

Send in a PR and I'll check it.

Connum commented 4 years ago

See #267

ahmadawais commented 4 years ago

This issue must have been solved via #267.

Successfully published:

You do have to manually update the init.php file like this.