Query String Sort allows customers to increase cache hits on resources with varied query strings.
By default, Cloudflare’s cache treats resources as distinct if their URL query strings are in a different order. For instance, these resources are cached separately:
Query String Sort increases cache-hit rates by first sorting query strings into a consistent order before checking the Cloudflare cache. If two query strings exist with the same name, the URL is sorted by the parameter value. For example:
are sorted to:
Enable Query String Sort
Query String Sort is available in the Cloudflare dashboard under the Caching app.
- Log into your Cloudflare account.
- Choose the appropriate domain for which you want to enable Query String Sort.
- Click the Caching app.
- Scroll down to Enable Query String Sort
Toggle the switch to On.
Unexpected behavior with WordPress admin pages
When a site or an application requires exact query string ordering, enabling Query String Sort might cause unexpected behavior.
For example in the WordPress admin UI, you might notice any of the following behaviors:
- No media appear in the Media Library
- Inability to customize the site via Appearance > Customize
- Inability to drag any widget to a sidebar in Appearance > Widgets
- Inability to edit menus in Appearance > Menus
Identifying the problem
The screenshot below shows an example where resources in the Media Library are not rendered correctly and the browser debugging console reveals that the page is throwing an error:
When the page
load-scripts.php loads, the browser sends a request to Cloudflare for:
With Query String Sort enabled, Cloudflare will then sort the parameters and values in the request query string, resulting in the following:
Note that the
load parameters were swapped, as
equest should come before
hoverIntent when alphabetically ordered.
When this happens, you will most likely find errors in the browser console, such as:
_____ is not defined at load-scripts.php?c=0&load=...
This type of error indicates that Query String Sort is inadvertently breaking some WordPress admin page functionality.
After sorting, the query then goes to Cloudflare's cache infrastructure (and to the origin server, if the resource is not in the Cloudflare cache or is not cacheable). The origin server then serves the concatenated scripts, which are ordered differently. Because scripts might depend on other scripts, this process might break dependencies.
Responding to the issue
Start by analyzing your site or application behavior around the use of query strings. Do you have assets served with multiple possible arrangements of query strings?
For example, you might have an image resizing endpoint or a search form, where the order of query parameters might vary—like width, height, version, etc., yet a unique parameter combination points to a single relevant asset.
To minimize problems, consider:
- Disabling Query String Sort for the site if you’re sure that this feature does not add value to any part of your site. Cloudflare disables this option by default in the Caching app.
- Use Cloudflare Page Rules to enable Query String Sort for URLs where preserving the query string parameter order is not important.
- Alternatively, use Cloudflare Page Rules to disable Query String Sort for URLs where a specific parameter order is required. For example, disable Query String Sort for
example.com/wp-admin/load-scripts.php*or any URLs with similar requirements (replace example.com with your domain name).
To learn more about Page Rules, visit Understanding and Configuring Cloudflare Page Rules.