• Resolved nestormarius

    (@nestormarius)


    Hello, I would like to set data-jetpack-boost="ignore" to a script when using Jetpack Boost’s “Defer Non-Essential JavaScript” feature.

    However, my issue is that the script is enqueued in the site using wp_enqueue_script. How do I append data-jetpack-boost="ignore" to wp_enqueue_script?

    Thanks!

    • This topic was modified 1 year, 5 months ago by nestormarius.
Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Support Animesh Gaurav (a11n)

    (@bizanimesh)

    Hey @nestormarius – here is the example how this can be done:

    function wpdemo_enqueue_scripts() {
        wp_enqueue_script( 'wpdemo', get_template_directory_uri().'/js/respond.min.js' );
    }
    add_action( 'wp_enqueue_scripts', 'wpdemo_enqueue_scripts' );
    function add_data_jetpack_boost_tag( $src, $handle ) {
        if ( $handle !== 'wpdemo') {
            return $src;
        }
        return str_replace( ' src', ' data-jetpack-boost="ignore" src', $src );
    }
    add_filter( 'script_loader_tag', 'add_data_jetpack_boost_tag', 10, 2 );

    In add_data_jetpack_boost_tag() you will have to change the handle in the?if?check.

    Plugin Support Tamirat B. (a11n)

    (@tamirat22)

    Hello @nestormarius

    It’s been one week since this topic was last updated. I’m going to mark this thread as solved. If you have any further questions or need more help, you’re welcome to open another thread here. Cheers!

    Thread Starter nestormarius

    (@nestormarius)

    Hi @bizanimesh and thanks for your reply. Could you please help me add data-jetpack-boost=”ignore” to the code below?

    client_main.js is the JS file I want Jetpack Boost to ignore when I enable the Defer Non-Essential JavaScript option.

    function darkmysite_client_enqueue()
    {
    if($this->darkmysite_is_dark_mode_allowed()){
    wp_enqueue_script('darkmysite-client-main', DARKMYSITE_JS_DIR.'client_main.js', array(), DARKMYSITE_VERSION);
    }
    }

    Thank you!

    • This reply was modified 1 year, 1 month ago by nestormarius.
    • This reply was modified 1 year, 1 month ago by nestormarius.
    • This reply was modified 1 year, 1 month ago by nestormarius.
    Plugin Support Alin (a11n)

    (@alinclamba)

    Hi @nestormarius,

    To append the data-jetpack-boost="ignore" attribute to your script tag, you can use the script_loader_tag filter to modify the script’s HTML output. Here’s how you can modify the code for your client_main.js file:

    function add_data_jetpack_boost_to_client_main($tag, $handle, $src) {
        // The handle for your script
        if ($handle === 'darkmysite-client-main') {
            $tag = str_replace( ' src', ' data-jetpack-boost="ignore" src', $tag );
        }
        return $tag;
    }
    add_filter('script_loader_tag', 'add_data_jetpack_boost_to_client_main', 10, 3);
    

    Just place this code snippet in your theme’s functions.php file or in a site-specific plugin. This will add the data-jetpack-boost="ignore" attribute to the client_main.js script when it’s enqueued.

    Please make sure you have a solid backup system in place before making this kind of changes, just in case.

    Hope it helps.

    Thread Starter nestormarius

    (@nestormarius)

    Hi @alinclamba

    Thank you so much for your reply. The code snippet worked and now the data-jetpack-boost="ignore" attribute has been added to the client_main.js file when I look at the HTML output.

    However, when I enable the “Defer Non-Essential JavaScript” option in Jetpack Boost, the client_main.js script no longer works as expected, therefore the site no longer functions correctly.

    I should mention the fact that the script gets a ?m=1695225095 appended to the end of the file name (e.g. client_main.js?m=1695225095)

    <script type='text/javascript' data-jetpack-boost=”ignore” src='/wp-content/plugins/darkmysite/assets/js/client_main.js?m=1695225095'></script>

    Any ideas?

    Plugin Support Alin (a11n)

    (@alinclamba)


    Hi @nestormarius,

    Thanks for the reply! A quick suggestion: try adding the data-jetpack-boost="ignore" tag to darkmysite_inline_js to see if this helps with the issue.

    Just a heads-up, due to the custom code and the specific support required, we’re at our limit for further assistance on this. We really hope this suggestion makes a difference, and we appreciate your understanding.

    Wishing you the best with your adjustments!

    Best regards!

    • This reply was modified 1 year, 1 month ago by Alin (a11n).
    Thread Starter nestormarius

    (@nestormarius)

    Hi @alinclamba

    No, it still doesn’t work. Anyway, thanks for your help.

    Best regards!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘data-jetpack-boost=”ignore” issue’ is closed to new replies.