# Autopsy #04: The Zombie Bundle After a Production Deployment

- **Canonical URL:** https://frontend.rocks/autopsies/zombie-bundle-after-deploy/
- **Case:** #04
- **Failure Mode:** Silent Data Corruption
- **Production Symptom:** Active tabs throw ChunkLoadError or corrupt local IndexedDB state after a production deploy.
- **Last Verified:** 2026-11-16

## Causal Chain
1. [Service Worker Lifecycle](https://frontend.rocks/concepts/service-worker-lifecycle.md): An outdated Service Worker keeps serving stale HTML and chunk manifests to open tabs during deploy.
2. [HTTP Caching Directives](https://frontend.rocks/concepts/http-caching-directives.md): Immutable asset headers and stale-while-revalidate HTML responses split the module graph across versions.
3. [ES Modules and Live Bindings](https://frontend.rocks/concepts/es-modules-live-bindings.md): Cached ESM modules retain live bindings to stale exports while new dynamic imports fail or mismatch.
4. [Back/Forward Cache (bfcache)](https://frontend.rocks/concepts/bfcache-navigation.md): Restoring a page from the back-forward cache resumes an old JS runtime after IndexedDB already upgraded.
5. [Client Storage Versioning & Zero-Downtime Migrations](https://frontend.rocks/concepts/client-storage-schema-migrations.md): An older tab writes records using a deprecated schema after another tab bumped the IndexedDB version.

## Primary Sources
- https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API/Using_Service_Workers
- https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control
- https://web.dev/articles/bfcache
- https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API/Using_IndexedDB

---
## Incident Report

At 10:00 UTC, CI deployed a routine workspace schema migration (`v2`) along with new content-hashed JavaScript bundles and pruned old `v1` chunks from the CDN bucket. Fresh incognito smoke tests passed immediately.

Within 12 minutes, RUM telemetry and customer support escalated a multi-tab failure affecting long-lived sessions.

- **T+00:00**: `workspace-app@2026.11.16.2` rolls out; CDN purges `v1` lazy chunks.
- **T+04:00**: `ChunkLoadError` rate spikes to 4.8% among users who opened a second tab after the deploy.
- **T+12:00**: 310 support tickets report missing offline drafts after users clicked the Back button from an external OAuth redirect.
- **T+18:00**: IndexedDB telemetry confirms `v1` tabs writing deprecated record shapes into `v2` object stores.

## The Red Herring

The first Slack reflex was to call `self.skipWaiting()` inside the Service Worker `install` handler and attach a `window.location.reload()` listener on `ChunkLoadError`. Locally, hard-reloading a single tab cleared the error.

In production, that patch worsened the outage. `skipWaiting()` hijacked active tabs mid-transaction, while back-button navigations still restored frozen `v1` JavaScript heaps from `bfcache` without triggering a network fetch or Service Worker intercept, writing legacy records into the upgraded `v2` database.

## Causal Chain

A deploy skew incident is a five-layer state divergence across worker registration, HTTP cache headers, the ECMAScript module map, browser history snapshots, and client-side storage schemas.

1. The active Service Worker serves the cached `v1` `index.html` manifest to existing tabs.
2. CDN `Cache-Control` rules either 404 pruned chunks or serve mixed-version assets.
3. The browser ESM module map preserves live bindings to `v1` exports alongside `v2` dynamic imports.
4. `bfcache` restores a frozen `v1` heap after another tab upgraded the origin's database.
5. The zombie `v1` runtime writes unmigrated records into a `v2` IndexedDB object store.

## Failure Simulator

Step through the five-stage deploy skew sequence in the simulator below. Observe how `tabAVersion`, `tabBVersion`, and `idbVersion` diverge when Tab B upgrades IndexedDB while Tab A remains pinned to `v1` by the Service Worker and `bfcache`.

## Layer Dissection

### 01 → 02: Service Worker Ownership Meets HTTP Cache Skew

Under the [Service Worker Lifecycle](/concepts/service-worker-lifecycle), a newly deployed `sw.js` enters `waiting` until every client controlled by `v1` unloads. While `v1` controls Tab A, it intercepts navigation requests and returns cached `index.html` referencing `v1` chunk hashes.

When traffic hits the CDN edge directly, [HTTP Caching Directives](/concepts/http-caching-directives) compound the split:

- `index.html` with `stale-while-revalidate` returns yesterday's manifest during the stale window.
- Hashed chunks carry `Cache-Control: public, max-age=31536000, immutable`, so the browser skips revalidation.
- Once the CDN prunes old build artifacts, `v1` HTML requests a `v1` lazy chunk and receives HTTP 404.

### 02 → 03: HTTP Split Corrupts Live ESM Bindings

When a dynamic `import()` resolves against a newer build while a shared runtime chunk resolves from cache, the module loader does not clone values. Under [ES Modules Live Bindings](/concepts/es-modules-live-bindings), every importer holds a read-only reference to the exporter's live memory slot.

If an unhashed entrypoint survives in cache while a feature chunk loads from `v2`, `v2` code reads `v1` schema constants or throws `SyntaxError` on missing named exports during module linking.

### 03 → 04: Module Skew Survives Inside bfcache

When a user navigates to an external OAuth page, the browser snapshots the entire `v1` JS heap and DOM into memory. Under [bfcache Navigation](/concepts/bfcache-navigation), pressing Back restores that snapshot synchronously (`pageshow` with `event.persisted === true`).

No HTTP request fires and no Service Worker `fetch` event executes. Meanwhile, Tab B has already booted `v2` from the network and executed `onupgradeneeded` on the shared origin database.

### 04 → 05: Zombie Runtime Corrupts Client Storage Migrations

IndexedDB is origin-scoped, not tab-scoped. When Tab B bumps the database version from `1` to `2`, [Client Storage Schema Migrations](/concepts/client-storage-schema-migrations) require every active tab to close its handle on `versionchange` before the upgrade proceeds.

A `bfcache`-restored tab that ignored `versionchange` still executes `v1` serialization code. Opening a new readwrite transaction against the upgraded `v2` store writes `v1`-shaped objects that violate `v2` index key paths and corrupt shared origin state.

## Architectural Fix & Invariants

```ts
const APP_VERSION = '2026.11.16.2';

window.addEventListener('pageshow', (event) => {
  if (event.persisted && localStorage.getItem('active_build') !== APP_VERSION) {
    window.location.reload();
  }
});

const openReq = indexedDB.open('workspace_db', 2);
openReq.onsuccess = () => {
  const db = openReq.result;
  db.onversionchange = () => {
    db.close();
    window.location.reload();
  };
};
```

**Architectural Invariants:**
- **Immutable chunks, zero-stale HTML**: Serve `index.html` with `Cache-Control: no-cache` and retain previous build chunks on the CDN for at least 24 hours after deploy.
- **No blind `skipWaiting()`**: Activate a waiting Service Worker only when no active transactions are open, then reload all controlled clients atomically.
- **Mandatory `versionchange` teardown**: Implement `onversionchange` on every `IDBDatabase` handle to call `db.close()` and halt writes from outdated bundles.
- **Revalidate on `persisted` restore**: Compare the running `APP_VERSION` against the active origin version on every `pageshow` event where `event.persisted === true`.

**Golden Rule:** A frontend deploy is never atomic across open tabs and `bfcache`; fence every storage write behind a versioned `onversionchange` and `pageshow` check.
