Disabling Leaflet CSS on the Frontend

Support General Disabling Leaflet CSS on the Frontend

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #43876
    AMXAMX
    Participant

    Hi,

    I have and OpenStreetMap field, but on the frontend I use only Mapbox, so I don't need leaflet.css outside of the admin area.

    But I get this in the head:

    <link rel="stylesheet" id="leaflet-css" href="https://unpkg.com/[email protected]/dist/leaflet.css" media="all" />

    I would like to disable that, because it is render-blocking and my score in PageSpeed Insights is affected.

    I tried this, but it doesn't seem to work:

    function dequeue_leaflet_css() {
       
        if (!is_admin()) {
            // Dequeue Leaflet CSS
            wp_deregister_style('leaflet');
            wp_dequeue_style('leaflet'); 
            wp_deregister_style('leaflet_css');
            wp_dequeue_style('leaflet_css'); 
    		
        }
    }
    add_action('wp_enqueue_scripts', 'dequeue_leaflet_css'); 

    Greetings,
    Tom

    #43885
    PeterPeter
    Moderator

    Hello,

    The leaflet css is a dependency of the rwmb-osm css so it does not work when you dequeue this css file. Following this comment from a contributor https://developer.wordpress.org/reference/functions/wp_dequeue_style/#comment-5637

    #43893
    AMXAMX
    Participant

    All right, so the standard method of dequeuing doesn't work in this case. Any ideas how to achieve the goal?

    #43901
    PeterPeter
    Moderator

    Hello,

    The leaflet css only loads when rendering the field so if you remove it, the OSM map field does not work properly. There isn't a way to remove the CSS unless you modify the source code directly.

    #43941
    AMXAMX
    Participant

    There are many scenarios where you don't need leaflet.css at all on the front-end. The field itself will work fine if the stylesheet is disabled on the front-end and kept in the admin area.

    If someone uses, for example, Mapbox to render the field, that css on the front-end has no purpose, it is just bloat.

    Also, as the file is loaded from an external server, I am not sure if it is OK, in the context of GDPR.

    There must be some way to deactivate it...

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.