Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Block CSS Loading in Classic Themes #39

Open
mustafauysal opened this issue Jan 30, 2025 · 0 comments
Open

Improve Block CSS Loading in Classic Themes #39

mustafauysal opened this issue Jan 30, 2025 · 0 comments

Comments

@mustafauysal
Copy link
Member

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 );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant