DEV Community

Cover image for Autopsy #04: The Morning Yesterday's Bundle Corrupts Today's Database
frontend.rocks
frontend.rocks

Posted on Edited on Originally published at frontend.rocks

Autopsy #04: The Morning Yesterday's Bundle Corrupts Today's Database

Saving S3 pennies, breaking yesterday's tabs

At 10:00 on Wednesday, Marc deployed v2 of the workspace IndexedDB schema along with a tidy CI step called chore: prune old CDN chunks on deploy to stop old build artifacts from piling up on S3. He opened an incognito window, clicked through onboarding in three seconds, saw the new workspace columns load cleanly, and went back to his backend tickets.

Twelve minutes later, Thomas walked over from product with his laptop open. People who had left a tab open since Tuesday were seeing ChunkLoadError whenever they opened a lazy-loaded settings panel. Worse, users returning from an OAuth redirect via the browser Back button watched their offline drafts vanish with no error message at all.

Why skipWaiting() made it worse

Julie's first instinct was to force everyone onto the new version immediately so old code could not linger in memory. She added self.skipWaiting() inside the Service Worker install handler and wired a global error listener to call window.location.reload() on ChunkLoadError.

In local dev with one tab open, reloading cleared everything cleanly and the new v2 bundle booted right up.

In production, it turned a chunk mismatch into data corruption. Calling self.skipWaiting() during the Service Worker lifecycle swapped the controller out from under v1 tabs that were still running in memory. If a v1 tab was mid-sync or loading a lazy chunk, the new Service Worker served v2 manifests to a v1 runtime, while an unguarded location.reload() threw away unsaved editor buffers. Meanwhile, users hitting Back after OAuth did not hit the network or the Service Worker at all.

Two versions sharing one browser

What happened across those tabs crossed five layers of browser state, because a browser tab is not a stateless terminal that restarts when CI turns green.

First, pruning v1 chunks broke any v1 tab whose HTML had been cached or served before 10:00. With proper HTTP caching directives, index.html uses Cache-Control: no-cache while hashed JS bundles use public, max-age=31536000, immutable and stay on the CDN for at least N-1 and N-2 releases. Deleting old chunks while old HTML still points to them breaks the module dependency graph—and leaves cached ES module live bindings pointing at exports that no longer resolve—the moment a user triggers a dynamic import().

Second, the OAuth Back-button users were hitting the back-forward cache (bfcache). When a browser restores a page from bfcache, it thaws the exact v1 JavaScript heap that was frozen before the redirect. If another tab had already opened after 10:00 and upgraded IndexedDB to v2, that thawed v1 tab woke up and wrote v1 records straight into a v2 object store during client storage schema migrations.

Giving old tabs an orderly exit

Interactive Failure Simulator — Tap to step through the live model

Closing the database before the banner

First try on onversionchange: when a new tab triggered upgradeneeded, the old tab displayed the reload banner, waited for an in-flight flushDraft() async write to finish, and then called db.close().

db.onversionchange = () => {
  showReloadBanner();
  pendingFlush.finally(() => db.close()); // blocked upgradeneeded for 15s
};
Enter fullscreen mode Exit fullscreen mode

In IndexedDB, upgradeneeded in the new tab cannot start until every existing connection closes. Waiting for an async flushDraft() kept the old connection open and stalled the new tab in onblocked for 15 seconds, which looked like a frozen login screen. We changed onversionchange to abort the in-memory write queue and call db.close() synchronously on the spot, letting the new tab upgrade in 12 milliseconds while the old tab showed the reload prompt.

What stayed in the codebase

A web deploy is not an atomic swap; it is a window where two versions of your code share the same browser.

Marc's CDN cleanup script had saved roughly \$1.40 a month in S3 storage, and cost us two afternoons of restoring corrupted customer drafts from server logs. We now keep the last three releases on S3 by policy, and ChunkLoadError alerts have stayed at zero across twelve consecutive deploys.

Top comments (0)