WooCommerce Product Filter Not Working With WP Rocket and Caching
When a WooCommerce product filter is not working with WP Rocket or another optimization plugin, the pattern is almost always the same: everything works while you are logged in, and for visitors the clicks do nothing, the loader spins forever, or the price slider shows up as a plain text field. HUSKY sends the same code to both. The difference is that caching and JavaScript optimization usually skip logged-in administrators, so you never see the optimized page your visitors get. Below: how to read the error in a minute, what exactly to exclude in WP Rocket, LiteSpeed Cache, Autoptimize, SiteGround Optimizer and Cloudflare, and how to stop the page cache from breaking AJAX filtering with a nonce error.
Why the filter works for admins and fails for visitors
The filter's front end is not one script file but a chain that must run in a fixed order:
- jQuery, which every filter script depends on.
- An inline settings block printed into the page (in the source it is
<script id="woof_front-js-before">). It defines the variables the filter needs:woof_current_values,woof_is_ajax,swoof_search_slug,woof_ext_init_functions, the security tokenwoof_front_nonceand others. - The plugin files from
/wp-content/plugins/woocommerce-products-filter/:js/front.js, which reads those variables, the scripts of the filter elements and extensions, and jQuery add-ons such as iCheck (checkbox skins), ion.rangeSlider (sliders) and plainOverlay (the loading overlay).
Optimization and caching break this chain in three ways. Optimization changes the order, for example by delaying the files but not the inline block, or the other way round. It moves or rewrites the inline block while combining or minifying. And a page cache stores HTML that contains data with an expiry date: the security token. Most caching and optimization plugins serve logged-in users a page that is neither cached nor optimized, so the admin view proves only that the filter itself works.
Read the browser console first
Open the shop in a private window, where you are not logged in. Press F12 (Cmd+Option+I on a Mac), open the Console tab, reload the page and click a filter. The first red error tells you where to look.
| Console message | What it means |
|---|---|
woof_current_values is not defined, the same for woof_is_ajax, swoof_search_slug, woof_ext_init_functions (Firefox: assignment to undeclared variable, Safari: Can't find variable) | The inline settings block did not run before front.js: it was delayed, moved, removed or broken |
jQuery is not defined | jQuery is delayed or deferred, but code that needs it ran earlier |
iCheck is not a function, plainOverlay is not a function, ionRangeSlider is not a function | The add-on file did not load, or a second copy of jQuery loaded after it and replaced the jQuery object |
woof_husky_txt is not defined | The data block of the text search was stripped or separated from its file |
SyntaxError pointing to the inline block | Invalid code in the inline block, often from the plugin's own custom JavaScript field (see below) |
Identifier 'woof_front_nonce' has already been declared | The settings block ran twice: an optimizer or a theme with AJAX page transitions re-ran inline scripts |
"nonce error" is not valid JSON | The page came from a cache and its security token has expired |
Unexpected token '<' ... is not valid JSON | The server answered the filter request with HTML: a PHP notice, a firewall page or a cached page |
Bad escaped character in JSON | Something rewrote the AJAX answer on its way to the browser |
No error at all, but the slider is a plain input, the + openers of hierarchical lists are missing and clicks do nothing: the scripts have not run yet. This is how "delay JavaScript until user interaction" features look. View the page source and search for woof_front: if the script tags carry a type such as rocketlazyloadscript or litespeed/javascript, an optimizer is holding them back. If woof_front is not in the source at all, check the field Init plugin on the next site pages only (Advanced → Code): when it lists pages, the plugin loads only there.
What to exclude from JavaScript optimization
The rule is the same in every tool: jQuery, the inline blocks and the plugin files must stay together and in their original order. Excluding only front.js is a common half-fix that does not work, because the settings block and jQuery are still delayed. These three lines cover everything:
/wp-includes/js/jquery/
woocommerce-products-filter
woof_
The first line matches jQuery and jQuery UI (the WooCommerce-style price slider uses it). The second matches every file of the plugin and its extensions. The third matches the inline blocks: their ids and the variables inside them start with woof_. Exclusion fields of optimization plugins usually match a keyword against both the file URL and the content of inline scripts.
- WP Rocket. In File Optimization, add the lines to the excluded files of Delay JavaScript execution; if the filter still breaks, add them to the exclusions of Load JavaScript deferred as well, or switch that option off. If Optimize CSS delivery is set to remove unused CSS, add the plugin's styles and the
.woofclasses to its CSS safelist: many filter classes appear only after JavaScript runs, so a scan of the static page treats them as unused. - LiteSpeed Cache. JS Combine and Load JS Deferred (deferred or delayed) are the usual causes; add the lines to the JS excludes and the deferred/delayed excludes. If the filter request returns but the products do not refresh and the console shows
Bad escaped character in JSON, switch CDN Mapping off: in confirmed cases it rewrote the AJAX answer. - Autoptimize. When JavaScript is aggregated, jQuery moves into the combined file at the end of the page, while inline code stays where it was and runs too early. Keep
js/jquery/jquery.min.jsandwoocommerce-products-filterin Exclude scripts from Autoptimize. - SiteGround Speed Optimizer, WP-Optimize and similar. Combine, defer and "delay inline scripts" options all need the same exclusions, or must be off.
- Cloudflare Rocket Loader. It loads all scripts asynchronously, inline blocks included, and it also stops the Filter front builder extension from starting. Turn it off in Cloudflare's speed settings, then purge the Cloudflare cache.
- HTML minification that "removes unnecessary characters" can damage inline scripts. Switch off its inline JavaScript minification.
- Theme or snippet code on the
script_loader_tagfilter. WordPress passes the inline block through this filter together with the script tag, so code that strips attributes or replaces quotes there can corrupt it. Remove such code or limit it to other handles.
Two settings inside the filter
JavaScript code after AJAX is done (WooCommerce → Settings → Products Filter → Advanced → Code) is inserted into the inline settings block as the body of a function. A typo there, such as an unclosed bracket, breaks the whole block, and every filter variable becomes "not defined". Empty the field to test; to run code after filtering, the woof_ajax_done event is a safer place.
Optimize loading of HUSKY JavaScript files (Advanced → Options) switches between two ways of loading: No loads front.js plus four separate scripts for radio buttons, checkboxes and drop-downs; Yes loads one compressed file, js/front_comprssd.js, in the footer. Some optimizer setups work with one value and not the other. Try both, purging all caches after each change.
Page cache and the "nonce error"
In AJAX mode (Try to ajaxify the shop on the Options tab, or a [woof_products is_ajax=1] page) and with the ajax_redraw attribute, the filter asks the server for new products and sends the token woof_front_nonce taken from the page. The server checks it and, if it is not valid, answers with the plain text nonce error. The script expects JSON, cannot read that answer, and the loader stays on screen.
The token is created when the HTML is generated. For logged-out visitors WordPress nonces are valid for at least 12 and at most 24 hours. A page served from a cache older than that carries a dead token. That is why the problem appears on cached pages a day after the cache was built, and why a reload sometimes helps: it happens to hit a fresh copy. Clearing the cache fixes it for a few hours only. Lasting fixes:
- Keep the cache lifespan of HTML pages under 12 hours. WP Rocket's default lifespan of 10 hours is safe; long-lived HTML caching at a CDN is the typical offender.
- Do not cache shop and category HTML at the CDN. With Cloudflare, add a cache rule that bypasses the cache for the shop, the
/product-category/URLs and any other page with the filter, and keep your WordPress page cache, which respects its own lifespan. Bypassing only filtered URLs is not enough: the token the visitor uses comes from the page where filtering starts. - Extend the lifetime of this one token. Since WordPress 6.1 the nonce_life filter receives the action name, so the filter's token can live longer without touching any other nonce on the site. The token protects only the public product listing request.
/*
* HUSKY: keep the filter token valid longer than the page cache lifespan.
* A token is valid for at least half of this time (here: 2 days).
* Child theme functions.php or a code snippets plugin.
*/
add_filter('nonce_life', function ($life, $action = -1) {
if ('woof_front_nonce' === $action) {
return 4 * DAY_IN_SECONDS;
}
return $life;
}, 10, 2);
Plain redirect mode, without AJAX and without ajax_redraw, does not use the token at all: each filter change loads a new URL, and the page cache can serve filtered URLs like any other page. If you call the filter from your own code, for example a custom jQuery request to woof_draw_products, the request data must include nonce_filter: woof_front_nonce, or the server answers nonce error even without any cache.
Stale filters and other cache side effects
- Changed filter settings do not show up. The settings are part of the cached HTML and of the inline block. Purge every cache layer after saving them: the cache plugin, the server cache (Varnish, Nginx, host cache), the CDN, and then hard-refresh the browser (Ctrl+F5, Cmd+Shift+R on a Mac).
- A filtered link opens with nothing selected, or a clean shop opens with old selections. The cache treats URLs with different query strings as one page. Every
?swoof=1&...URL must be cached separately or not at all, so never set the cache to ignore query strings on shop URLs. - The mobile filter appears on desktop, or the other way round. The mobile mode of the filter and some extensions decide on the server, from the browser's user agent, what to print. A page cached for a phone must not be served to a desktop: enable separate cache files for mobile devices.
- The first visit to a new filter combination is slower: that URL has no cached copy yet. For general speed, see speeding up filtering on large catalogs.
A safe order for testing
- Work in a private window, logged out, with the console open.
- Switch off all JavaScript optimization (minify, combine, defer, delay, Rocket Loader) and purge every cache layer. If the filter now works, optimization is the cause. If it still fails, the cause is elsewhere: read the first console error, and test with a default theme on a staging copy.
- Turn the options back on one at a time, purge, and test after each. When one breaks the filter, add the three exclusion lines to that option, or leave it off.
- In AJAX mode, check the filter again on a page that has been in the cache for a day. No nonce error means the cache lifespan or the token lifetime is right.
- Test on a phone as well as on a desktop, and on the second page of results.
If the filter still works only for logged-in users after these steps, open a topic on the HUSKY support forum with the page link, the first console error and the list of optimization options you have enabled, and we will look at it.