cabrerahector / wordpress-popular-posts

WordPress Popular Posts - A highly customizable WordPress widget that displays your most popular posts.
https://wordpress.org/plugins/wordpress-popular-posts/
GNU General Public License v2.0
280 stars 82 forks source link

Defer / async loading of wpp.js? #279

Closed E-VANCE closed 3 years ago

E-VANCE commented 3 years ago

I noticed that Google PageSpeed advises to defer the parsing of JS in order to improve on the loading speed – is there a (technical) reason for WPP not to implement this by heart? Maybe the views won't be counted properly...?

I know that I can overwrite the loading in my WP instance / add the async-tag, just wanted to check in beforehand.

Thanks & regards, Henning

cabrerahector commented 3 years ago

Hi Henning,

The reason why WPP's script doesn't use defer/async is to make sure the plugin can track pageviews as soon as the browser is able to download and parse its script. This also reduces the chances of other scripts interfering.

You can add the async/defer attribute without having to modify plugin's code though. For example, I'm using the script_loader_tag filter hook to do so on my own website.

E-VANCE commented 3 years ago

OK, gotcha – early parsing for a plugin like yours to ensure the views are counted properly makes sense. Will defer via script_loader_tag and many thanks for your feedback.

Keep up the great work, much appreciated!