Blog · web development

I saved 148 KB and broke half the site. A story of one optimisation

2026-11-16

Good idea, bad execution

A company WordPress site was loading a carousel library on every single page. 148 KB of JavaScript sent out even where no carousel existed – which was most of the site. Textbook optimisation material.

I did it the usual way: check whether the page contains any of the blocks that use a carousel, and only then enqueue the library.

if (page_has_block(array(
    'acf/banner-carousel',
    'acf/promotions',
    // ...and seven more
))) {
    wp_enqueue_script('swiper', /* ... */);
}

Measurement tools were happy. Fewer kilobytes, faster loads.

For a few days nobody reported anything. Then I opened the browser console on a completely different page.

What I failed to anticipate

Uncaught ReferenceError: Swiper is not defined
    at HTMLDocument.mainInit (main.min.js:1:485)

The site’s main script file called the library unconditionally, on line twenty-nine:

const heroSwiper = new Swiper('.bannerCarousel__slidesHolder', {
  // ...
})

I had made the library conditional, but the code using it still assumed it was there. On a page without a carousel it was not – and an exception fired.

That is where the real problem starts.

Why it was worse than it looked

In JavaScript an uncaught exception stops execution of the function at the point it occurred. It does not skip one line and carry on. It just ends.

That file had 370 lines. The exception fired on line 29. So everything below it – animations, homepage section logic, column handling, background switching, several other things – never ran at all on any page without a carousel.

And nobody reported it, because most of those things are details: something does not animate, something does not respond to hover. The site looks like it works. You have to open the console to see that half the script never started.

The fix

You could guard each call separately, but across seven call sites that gets messy. One wrapper is simpler:

function makeSwiper(target, options) {
  if (typeof Swiper === 'undefined') return null
  if (!target) return null
  return new Swiper(target, options)
}

Then replace every new Swiper( with makeSwiper(. When the library is missing, the function returns null and the rest of the file runs normally.

One trap in that replacement: in a few places the returned instance was used later – to manually sync pagination dots. It could now be null, so that needed checking too:

if (mobilePagination && promotionsSlider) {
  // ...
}

Without it I would have fixed one bug and introduced another, harder to find.

What I would do differently

Conditional loading is a good technique and I am not giving it up. But it has a precondition that is easy to forget: code using the library must account for its absence. Otherwise an optimisation turns into a regression that never raises its voice.

Specifically, next time:

  • Guard the calls first, change the loading second. The other order leaves a window where the site is broken.
  • Check the console on a page that does NOT get the new asset. I tested where the carousel exists – and there everything worked perfectly.
  • Mind exceptions in long files. An error on line thirty of a four-hundred-line file does not break line thirty. It breaks the three hundred and seventy that follow.
  • Do not trust silence. No user reports does not mean it works. It means nobody noticed.

The 148 KB saving stayed. Now everything else works too.

→ Back to all posts

Got a project to talk about?

Just tell me what you need - it does not have to be technical, that is my job. I will get back to you and tell you straight whether and how I can help.