Posts Tagged

remove query strings static resources

How to fix “remove query strings from static resources” for page speed

When checking your website against Google PageSpeed Insights, GTMetrix, Pingdom, or any other major site optimization checker, you may run into “Remove query strings static resources”. This message appears because adding a query string to any URL can cause proxies and caching mechanisms to ignore cache instructions, and always get the latest version of that file.

This is great if the file is constantly being updated, but since most static resources are unchanged for weeks at a time, it makes sense to take advantage of all caching available to us, and get a quick boost in speed for anyone visiting more than one page on the site.

A quick WordPress filter can be added to your functions.php file in your website’s theme:

function remove_script_version($src) {
return remove_query_arg("ver", $src);
add_filter("script_loader_src", "remove_script_version", 15, 1);
add_filter("style_loader_src", "remove_script_version", 15, 1);

This quick bit of code will remove the “?ver=4.x.x” from the end of all scripts and styles loaded properly through WordPress. Don’t be afraid to let me know in the comments if that worked for you.