The report was short: the search box in the header no longer suggests products. Not for everyone, and not on every visit. On a Swiss B2B store I maintain, this turned out to be a cookie banner, a script bundle and a single line of JavaScript, and the fix took more thinking than code.
What was going on
The theme loads its third-party libraries (a slider, a lightbox, a scroll helper and a polyfill) in one bundle, vendors.js. Its own code lives in app.js, which starts with one long jQuery ready handler: about 800 lines that set up the header, the search suggestions, the cart, the sliders and everything else.
The store's consent manager runs in automatic blocking mode: it holds back scripts it thinks may set cookies until the visitor agrees. When it held back vendors.js, jQuery itself was still there, because the theme loads it inline, so the ready handler ran as usual. Right at the top, it patched the slider library:
var oldCreate = window.Flickity.prototype._create;
window.Flickity did not exist, the line threw a TypeError, and JavaScript skipped the rest of the handler. In my test, with the bundle blocked only one initialiser out of 26 still ran. The search suggestions were just the most visible of the 25 that did not.
Reproduce it before fixing it
"Sometimes broken" bugs get much easier once you can switch them on. Chrome DevTools can block a single request: right-click vendors.js in the Network tab and choose Block request URL, then reload. The error appeared every time, and the search went quiet.
I then tested four cases and wrote the results down, checking something you can see on the page (the search suggestion wrapper appearing in the DOM), not just the console:
- bundle loaded, before the fix: no errors, 26 initialisers, suggestions work;
- bundle blocked, before the fix:
TypeError, 1 initialiser, no suggestions; - bundle loaded, after the fix: no errors, 26, suggestions work;
- bundle blocked, after the fix: no errors, 26, suggestions work.
Why not one big try/catch
Wrapping the whole handler in try/catch would have silenced the error, and every future error with it. Instead I went through the handler and found every place that used something from the bundle. It was not one place but six: the slider patch, the polyfill call, the lightbox defaults, the lightbox plugin, the scroll helper and the slider's jQuery plugin. Each got a check for exactly the library it needs:
// Flickity comes from vendors.js, which a consent manager can block.
if (window.Flickity) {
var oldCreate = window.Flickity.prototype._create;
// ...patch as before
}
if ($.fn.fancybox) {
$('.lightbox[rel="gallery"]').fancybox();
}
Shortened. One call further down needed no guard, because it only runs when the lightbox is already open, which cannot happen if the lightbox never loaded.
The comment matters as much as the check. The next developer who sees if (window.Flickity) should know why it is there, so they do not tidy it away.
Worth checking in your own theme
- Does your consent tool block scripts automatically? If so, block each of your theme's script files in DevTools, one at a time, and see what still works.
- Is there one giant ready or init function? One missing dependency at the top takes down everything below it. Independent features should start independently.
- Guard by feature, not by error. A check that names the library it protects keeps real bugs visible.
