gutenberg icon indicating copy to clipboard operation
gutenberg copied to clipboard

CSS style file from custom block plugin is not being enqueued after updating to v8.4.0

Open adsheyn opened this issue 6 years ago • 8 comments

Describe the bug I've made a custom block plugin and everything worked fine until I've updated Gutenberg plugin to version 8.4.0 from 8.3.0. Now, CSS file from my plugin is not being enqueued on the frontend. On the post edit screen that css file is enqueued and look OK.

Editor version (please complete the following information): I'm using the latest WP version with the Gutenberg plugin installed.

adsheyn avatar Jun 25 '20 19:06 adsheyn

Does it enqueue properly when the block is included on the page? We are testing optimization that loads block’s CSS on the front-end only when it’s used.

gziolo avatar Jun 26 '20 06:06 gziolo

Even if the block is on the page the CSS file is not included. Not sure how this new optimization works, maybe it works with single block plugins but not with multi-blocks plugins.

adsheyn avatar Jun 26 '20 09:06 adsheyn

Does it mean that multiple blocks share the same style and only one of them enqueues this this style? I’m trying to understand what use case are you describing.

gziolo avatar Jun 26 '20 10:06 gziolo

I'm using the Create Guten Block by Ahmad Awais to make custom blocks, so there is only one CSS file and one js script that register all the blocks at once.

I've checked the Atomic Blocks plugin and there is the same problem with not enqueuing CSS file on the frontend.

adsheyn avatar Jun 26 '20 11:06 adsheyn

Can you share the part of the code that registers and enqueues this CSS?

gziolo avatar Jul 03 '20 03:07 gziolo

Here is the content of the init file:

function mp_blocks_assets() {

	// Register block styles for both frontend + backend.
	wp_register_style(
		'mp-blocks-style', // Handle.
		plugins_url( 'dist/blocks.style.build.css', dirname( __FILE__ ) ), // Block style CSS.
		array(), // Dependency to include the CSS after it.
		null // filemtime( plugin_dir_path( __DIR__ ) . 'dist/blocks.style.build.css' ) // Version: File modification time.
	);

	// Register block editor styles for backend.
	wp_register_style(
		'mp-blocks-editor-style', // Handle.
		plugins_url( 'dist/blocks.editor.build.css', dirname( __FILE__ ) ), // Block editor CSS.
		array( 'wp-edit-blocks' ), // Dependency to include the CSS after it.
		null // filemtime( plugin_dir_path( __DIR__ ) . 'dist/blocks.editor.build.css' ) // Version: File modification time.
	);

	// Register block editor script for backend.
	wp_register_script(
		'mp-blocks-editor-script', // Handle.
		plugins_url( '/dist/blocks.build.js', dirname( __FILE__ ) ), // Block.build.js: We register the block here. Built with Webpack.
		array( 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-editor' ), // Dependencies, defined above.
		null, // filemtime( plugin_dir_path( __DIR__ ) . 'dist/blocks.build.js' ), // Version: filemtime — Gets file modification time.
		true // Enqueue the script in the footer.
	);


	// WP Localized globals. Use dynamic PHP stuff in JavaScript via `cgbGlobal` object.
	wp_localize_script(
		'mp-blocks-editor-script',
		'cgbGlobal', // Array containing dynamic data for a JS Global.
		[
			'pluginDirPath' => plugin_dir_path( __DIR__ ),
			'pluginDirUrl'  => plugin_dir_url( __DIR__ ),
			// Add more data here that you want to access from `cgbGlobal` object.
		]
	);

	register_block_type(
		'mp/mp-blocks', array(

			// Enqueue blocks.style.build.css on both frontend & backend.
			'style'         => 'mp-blocks-style',

			// Enqueue blocks.editor.build.css in the editor only.
			'editor_style'  => 'mp-blocks-editor-style',

			// Enqueue blocks.build.js in the editor only.
			'editor_script' => 'mp-blocks-editor-script',

		)
	);

}
add_action( 'init', 'mp_blocks_assets' );


function mp_register_block_categories( $categories, $post ) {
	return array_merge(
		$categories,
		array(
			array(
				'slug' => 'mp-blocks',
				'title' => __( 'MP Blocks', 'mp-blocks' ),
			),
		)
	);
}
add_filter( 'block_categories', 'mp_register_block_categories', 10, 2 );

adsheyn avatar Jul 03 '20 18:07 adsheyn

It must be related to #22754 where people started reporting similar issues after 8.4.0 release. I think the best way to go is to revert those changes and try a different approach in WordPress core.

gziolo avatar Jul 04 '20 06:07 gziolo

Now that #22754 was reverted, it shouldn't be an issue after the Gutenberg plugin release planned for later this week.

gziolo avatar Jul 06 '20 10:07 gziolo