Support

Account

Home Forums Gutenberg Load css is has block with the json file

Solving

Load css is has block with the json file

  • Hi

    I’m trying to display the css only if the block is used, I found a process with the register_block_type and passing the render file in this function.

    But I’m calling the block.json in my render function :

    
    $blocksGroup = [ 'block-actualites', 'block-formulaires-contact', 'block-timeline', 'block-produits', 'block-documents', 'block-membranes', 'block-certifications', 'block-indice', 'block-marches'];
    
    	foreach ( $blocksGroup as $block_nameGroup ) {
    	register_block_type( get_template_directory() .'/blocks/'.$block_nameGroup.'/block.json' );
    	}
    

    How to conditionnal load the css from the json if the block is used in the page ?

    thank you

  • I’m facing the same issue.
    The nearest to native solution that I have found is to add
    add_filter( ‘should_load_separate_core_block_assets’, ‘__return_true’ );

    This will include only css files of the blocks that are used in the page.
    But the css files are injected in the end of the body and I’m not sure if the user internet is slow how will effect the layout shift of the page

  • Oh interesting,
    Could you just give me the complete code with the add filter with my example ?

  • I tried to add the add_filter( ‘should_load_separate_core_block_assets’, ‘__return_true’ ); in function.php but it’s breaking the website

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.