Remove PageSpeed passive listeners warning in WordPress

Last edited:
October 29, 2023
Reading time:
3 mins

/

Blog

/

Speed

/

Remove PageSpeed passive ...

đź’ˇWe may earn a commision if you subscribe to a service from a link on this page.

PageSpeed Insights is a valuable tool for assessing and improving your website’s performance. Did you know it was created by the team that behind Chrome? It provides recommendations to optimize various aspects of your site, including how to eliminate “passive event listeners” to enhance user experience and load times. While these recommendations can significantly improve performance, WordPress users often encounter the “Remove PageSpeed passive listeners warning.” In this comprehensive guide, we’ll explore what passive event listeners are, why they matter, and how to remove this warning in your WordPress website, ensuring your site runs smoothly and ranks well in search engines.

Understanding Passive Event Listeners

Passive event listeners are a web development concept introduced to improve website performance. Event listeners are JavaScript functions that respond to user actions, such as clicks or scrolls. However, some event listeners can delay page loading, negatively impacting performance. Passive event listeners address this issue by telling the browser that the listener won’t prevent scrolling. This allows the browser to optimize scrolling performance.

Why Passive Event Listeners Matter

Passive event listeners play a crucial role in ensuring smooth and responsive websites. When an event listener is not marked as passive, the browser can’t optimize scrolling and may block page rendering. This can result in slower page load times and a less-than-ideal user experience. Google PageSpeed Insights detects these issues and recommends removing non-passive event listeners to enhance site performance.

Removing PageSpeed Passive Listeners Warning in WordPress:

To remove the “Remove PageSpeed passive listeners warning” in WordPress, you’ll need to make adjustments to your site’s files. Here’s how to do it:

1. Identify the file producing the error:

Start by identifying the JavaScript code that triggers the passive event listener warning. Obviously, that means to run Google PageSpeed Insights measurement on your website. Usually Google PageSpeed Insights results identify the file and present it, most commonly being jQuery. However if more than one files include passive listeners, you can typically find this code within your theme’s JavaScript files or added by plugins.

2. Add code to your functions.php or in the file with the problematic code:

If the only problem is jQuery, you can add the below code in the functions.php file of your child theme and the problem will be solved. However if more files in your WordPress website require the code, make sure you also add it in the respective files:

add_action('wp_footer','remove_passive_listeners_wprev',99);
function remove_passive_listeners_wprev(){
	?><script>
jQuery.event.special.touchstart = {
    setup: function( _, ns, handle ) {
        this.addEventListener("touchstart", handle, { passive: !ns.includes("noPreventDefault") });
    }
};
jQuery.event.special.touchmove = {
    setup: function( _, ns, handle ) {
        this.addEventListener("touchmove", handle, { passive: !ns.includes("noPreventDefault") });
    }
};
</script>
<?php
}

3. Test Your Site:

After making these changes, test your WordPress site to ensure it functions correctly. Make sure that scrolling remains smooth, and there are no errors in the browser’s developer console.

4. Clear Cache:

Clear your website’s cache to ensure that the changes take effect immediately.

5. Recheck PageSpeed Insights:

After making these modifications, run your website through Google PageSpeed Insights again to confirm that the “Remove PageSpeed passive listeners warning” has been resolved.

Get rid of that “remove passive listeners” warning!

Removing the “Remove PageSpeed passive listeners warning” in WordPress is crucial for optimizing your site’s performance and improving user experience. By understanding the importance of passive event listeners and how to mark event listeners as passive, you can ensure that your WordPress website loads quickly and efficiently. This not only benefits your site’s visitors but also boosts your search engine rankings, as Google rewards websites with faster load times. So, take the time to address this warning and enjoy the benefits of a well-optimized WordPress site.

Leave a Reply

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