Dev.to WebDev πŸ›  Dev πŸ‘ 2 πŸ“– 1 min read

How I Cut a 527 KB Vanilla JS Bundle by 81% and Fixed a Global Service Worker Caching Bug (220 Zero-Backend Micro-Labs)

While scaling DopaBrain's 220 browser-local behavioral psychology micro-labs, an audit surfaced three high-impact bottlenecks holding back performance, AdSense RPM, and Google Search crawl budget. Here is the exact engi

While scaling DopaBrain's 220 browser-local behavioral psychology micro-labs, an audit surfaced three high-impact bottlenecks holding back performance, AdSense RPM, and Google Search crawl budget.

Here is the exact engineering breakdown and before/after numbers (all verified with 90+ automated Playwright invariants and mutation tests):

1. Splitting a 526.8 KB Vanilla JS Hub by 81% (-> 102.4 KB)

Inlining all 12 translations (EN, KO, ZH, JA, ES, PT, DE, FR, ID, TR, HI, RU) for 220 tools inflated wellness-lab.js to 526.8 KB.

  • Kept compact English metadata inline inside wellness-lab.js (104,863 bytes)
  • Extracted the other 11 locales into on-demand /portal/js/wellness-lab-data/{lang}.json bundles (35.8 KB – 71.6 KB each)
  • Added deterministic promise-gated init (loadLocaleCopy(currentLang).then(applyI18n)) + enforced a < 120 KB bundle bloat ceiling mutation test inside our Playwright suite.

2. Fixing a Silent Root-Scope Service Worker (sw.js) Bug

Our root sw.js intercepted every request without checking method, origin, or HTTP statusβ€”meaning third-party scripts or 404 responses could get polluted:

self.addEventListener('fetch', (event) => {
  if (event.request.method !== 'GET') return;
  const requestUrl = new URL(event.request.url);
  if (requestUrl.origin !== self.location.origin) return;

  if (event.request.mode === 'navigate' || requestUrl.pathname.endsWith('.html')) {
    event.respondWith(fetch(event.request).catch(() => caches.match(event.request)));
    return;
  }

  event.respondWith(
    fetch(event.request).then((r) => {
      if (r && r.ok && r.type === 'basic') {
        const c = r.clone();
        caches.open(CACHE_NAME).then((cache) => cache.put(event.request, c));
      }
      return r;
    }).catch(() => caches.match(event.request))
  );
});

3. Direct-Bounce Shield & Focused Crawl-Budget Sitemap (2,389 -> 518 URLs)

  • Added an interaction/3s-visibility gate for zero-referrer automated scans without delaying real readers.
  • Pruned redundant XML sitemap bloat by 78% (2,389 -> 518 focused URLs) while preserving 100% of live HTML pages (HTTP 200), zero broken internal links, and zero client-side data collection.

Try the interactive suite (zero sign-up, zero backend):

πŸ“° Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.