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}.jsonbundles (35.8 KB β 71.6 KBeach) - Added deterministic promise-gated init (
loadLocaleCopy(currentLang).then(applyI18n)) + enforced a< 120 KBbundle 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 -> 518focused 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):
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.