Remove Query String from Static Resources

Tools such as Pingdom, GT Metrix, and PageSpeed often recommend to remove the query string from static resources. However, their recommendation often lack context, so they may or may not make sense for any random file URI.

WP Rocket provides a handy 1-click checkbox to remove query strings:

Remove Query Strings option located in Settings → WP Rocket → Static Files

However, not all query strings are created equal. Some can and will be removed, some could possibly, but will not be removed for safety reasons, and some cannot be removed without breaking your website.

Why should query strings be removed at all?

Query strings on file URIs prevent those files from being browser-cached. The browser would not even try to cache the file, but always download its newest version from the server.

Obviously, that’s safe, but not desirable. We want fast websites, so we want browser cache!

Browser cache and cache busting

Tip: Read Browser Caching, Explained In Plain English on our blog to learn more about how browser caching works.

“Cache busting” is the concept of not applying query strings to file URIs, but still indicating file changes to browsers.

One way of implementing cache busting is to simply change the file name when a file has been updated. The browser would check for the old file name and not find it, so it would remove the previously cached file from its cache and download the new file.

When WP Rocket removes a query string, it follows that exact concept: Not only gets the query string removed, but WP Rocket generates a copy of the original file with the query string value encoded into the file name.

Example:

// Original file URI with query string:
https://example.com/style.css?ver=1.0

// Copy generated by WP Rocket:
https://example.com/style-1.0.css

The files generated by WP Rocket are stored in a dedicated directory inside of the cache folder:

/wp-content/cache/busting/

Your source code will look something like this, with the option applied:

Not all query strings get removed, why?

WP Rocket only removes the default ?ver= query string from WordPress. This is by design:

Some plugin or theme might use a different query string for critical functionality. For example, the Photon feature of Jetpack would use a query string to fetch image sizes. So if WP Rocket removed all query strings, no matter what, it would likely break some functionality on a website. 

Query strings on external URIs (i.e. files from any other domain than your own) will not get removed due to general implications of  handling external resources.

Note: If you enable the Remove Query Strings option, it will (like LazyLoad) be applied even on pages where caching is disabled.

Older Version? Don’t use simplified solutions!

If you use a version of WP Rocket prior to 2.9, we strongly recommend not to remove query strings with a plugin or function that does not also encode the version into the URI. Unless proper cache busting can be guaranteed, query strings are imperative for your website to be displayed correctly.