How to Fix “Does not use passive listeners to improve scrolling performance ” Google PageSpeed WordPress

Have you ever tried optimizing your WordPress site and then Google Page Speed Analysis tells you that your website “Does not use passive listeners to improve scrolling performance “.  How DO you fix that? Let’s Discuss what passive listeners are and how to fix this in WordPress.

When you go to scroll a page and there’s a touch event listener (wheelmousewheeltouchstarttouchmove, etc.), the browser will not scroll immediately until it knows that the listener isn’t going to prevent the default scroll behavior. It doesn’t want to start scrolling prematurely, only to find out the listener eventually calls preventDefault()) because it wants to do something different.

Example: Cancelling the scroll altogether when interacting with an embedded Google Map

So this is where passive event listeners come in.

By adding a {passive: true} flag to the event listener, we can now tell the browser the listener will NOT cancel the default scroll behavior, and it’s safe to scroll immediately.

If you check your website on google page speed insights you may see this.

 

The problem is with the core WordPress comment system. WordPress (js/comment-reply.min.js) generating “non-passive” listeners. The script is responsible for comments UI features, in specific enabling users to comment in reply to another comment (i.e. nested commenting).

The problem with “non-passive” listeners is that they can slow down simple user interactions such as scrolling.

If you don’t allow nested comments anyway, you could simply remove this script from loading.

So How to fix this ?

Here is how you can remove the script from loading. But one thing to remember if you unload this, the nested comment will not work.

function wp_dereg_script_comment_reply(){wp_deregister_script( 'comment-reply' );}
add_action('init','wp_dereg_script_comment_reply');

You can add this code in your function.php and it will be done.

How to Add a Passive Listeners for this?

The following code should be added in your .js file, or inside a <script> tag in every page (.e.g in header.php) or even as a tag in TagManager if you use that.

 

//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
$('.comment-reply-link').click(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));
}

Conclusion

Hopefully, WordPress.org core contributors can come up with something official but till then we need to do this manually to make our site more google friendly and improve scroll performance using these code snippets to add Passive Event Listeners

Have questions or confused about something WordPress Related? Join Our Forum & ask a Question

2 thoughts on “How to Fix “Does not use passive listeners to improve scrolling performance ” Google PageSpeed WordPress”

  1. Thanks for the writeup!
    After following your recommendations, I faced an issue where the inline script was executing before jquery was loaded (due to async jquery delivery).

    I solved the issue by inline-ing and minifying the javascript you provided along with a defer tag so the script waits to execute until after async resources.

    {document.querySelectorAll(‘[data-commentid=\’\’+e+\’\’]’)[0].dispatchEvent(new Event(‘click’))})}function sleep(e){return new Promise(t=>setTimeout(t,e))}$(‘.comment-reply-link’).click(function(){if(!isScriptLoaded(‘/wp-includes/js/comment-reply.min.js’)){var e=document.createElement(‘script’);e.src=’/wp-includes/js/comment-reply.min.js’,e.onload=emRepClick($(this).attr(‘data-commentid’)),document.head.appendChild(e)}});”>

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top