Skip to content

Improve Block CSS Loading in Classic Themes #39

Description

@mustafauysal

Currently, in classic (non-block) themes, block-specific CSS is loaded globally, even when the block is not present on the page. This behavior is different from how block themes handle styles, where styles are only loaded when the block is used.

For better performance and to avoid unnecessary asset loading, we should ensure that block CSS is only enqueued when the block is actually present on the page.

Current workaround:

function magic_login_maybe_dequeue_block_style() {
    // Only run this check on the frontend, not in the admin panel.
    if ( is_admin() ) {
        return;
    }

    // Get the current post content
    global $post;

    // If the post content doesn't contain the block, dequeue the CSS.
    if ( $post && ! has_block( 'magic-login/login-block', $post ) ) {
        wp_dequeue_style( 'magic-login-login-block' );
        wp_deregister_style( 'magic-login-login-block' );
    }
}
add_action( 'wp_enqueue_scripts', 'magic_login_maybe_dequeue_block_style', 20 );

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions