DEV Community 👩‍💻👨‍💻

Cover image for Does not use passive listeners to improve scrolling performance
SeanAUS120
SeanAUS120

Posted on

Does not use passive listeners to improve scrolling performance

Here's a snippet to fix the "does not use passive listeners to improve scrolling performance" Google Pagespeed warning for Wordpress sites.

It comes from the jquery.min.js file that Wordpress serves and the scrollers are usually on pages with comment forms.

I get this warning on lots of my sites and finally I found something that worked to remove the warning.


function wp_dereg_script_comment_reply(){wp_deregister_script( 'comment-reply' );}
add_action('init','wp_dereg_script_comment_reply');
add_action('wp_head', 'wp_reload_script_comment_reply');
function wp_reload_script_comment_reply() {
    ?>
<script>
//Function checks if a given script is already loaded
function isScriptLoaded(src){
    return document.querySelector('script[src="' + src + '"]') ? true : false;
}
//When a reply link is clicked, check if reply-script is loaded. If not, load it and emulate the click
document.getElementsByClassName("comment-reply-link").onclick = function() {
    if(!(isScriptLoaded("/wp-includes/js/comment-reply.min.js"))){
        var script = document.createElement('script');
        script.src = "/wp-includes/js/comment-reply.min.js";
    script.onload = emRepClick($(this).attr('data-commentid'));
        document.head.appendChild(script);
    }
}
//Function waits 50 ms before it emulates a click on the relevant reply link now that the reply script is loaded
function emRepClick(comId) {
sleep(50).then(() => {
document.querySelectorAll('[data-commentid="'+comId+'"]')[0].dispatchEvent(new Event('click'));
});
}
//Function does nothing, for a given amount of time
function sleep (time) {
  return new Promise((resolve) => setTimeout(resolve, time));
}
</script>
    <?php
}
Enter fullscreen mode Exit fullscreen mode

Currently in use at:
Luxury Printing
Rush Flyer Printing
Printing Manhattan
Broadway Liquor
Windsurfing Fitness

Top comments (0)

🌚 Friends don't let friends browse without dark mode.

Sorry, it's true.