A user sent a screenshot with two timestamps. At 14:02 they started a 25-minute focus session. At 14:27 they opened the popup and saw 25:00, as if the session had never begun.
The badge had been counting down correctly for forty seconds. Then the worker died, and the only copy of the session state died with it.
The log, unedited
[14:02:10] worker boot [14:02:10] start(25m) -> interval 1000ms [14:02:11] tick remaining=1499s [14:02:12] tick remaining=1498s ... [14:02:41] tick remaining=1469s [14:02:41] service worker terminated (idle) [14:27:30] popup opened, worker boot [14:27:30] state={} -> render 25:00 Forty seconds of correct behavior, then silence. The interval had been alive for 31 ticks; the session had 1,469 seconds left.
Why the timer was the bug
The extension treated the interval as the source of truth. remaining lived in a module-level variable, decremented inside the callback. Nothing was written to storage, because writing every second felt wasteful.
That design has one assumption baked in: the process stays alive. MV3 removes it. When the worker is terminated, the variable, the interval handle and the callback closure all disappear together. The next popup render boots a fresh worker with an empty heap and falls back to the default 25:00.
The fix is not "persist more often". It is to stop storing the countdown at all.
Store the start, derive the rest
A Pomodoro session has three durable facts: when it started, how long it should run, and whether it was paused. The remaining seconds are a function of those facts and the current time.
type Session = { startedAt: number; durationMs: number; pausedAt?: number }; async function start(durationMs: number) { const session: Session = { startedAt: Date.now(), durationMs }; await chrome.storage.local.set({ session }); await chrome.alarms.create('pomodoro-tick', { periodInMinutes: 0.5 }); await chrome.alarms.create('pomodoro-done', { when: Date.now() + durationMs }); } function remaining(session: Session, now = Date.now()) { const end = session.startedAt + session.durationMs; return Math.max(0, end - now); } The badge is rendered from remaining(), not from a decrementing variable. Every alarm tick recomputes it from storage. A worker restart changes nothing, because nothing important was in memory.
When the pomodoro-done alarm fires, the handler checks that the session is still current, clears both alarms, writes a completed record and shows the notification. If the user paused in between, the completion check sees it and reschedules.
What the incident cost
The rewrite touched four files and added about ninety lines: a session module, two alarm handlers, a render function and a migration for the old shape. The bug had existed for three releases.
Two practices would have caught it earlier. First, a kill test: open chrome://serviceworker-internals, stop the worker mid-session, reopen the popup. Second, a rule that the UI never reads runtime state directly — it reads storage, and storage is the only interface between incarnations.
The trade-off we accepted
Alarms are not stopwatch-accurate. A 30-second period can drift, and Chrome may batch wakeups to save power, so the badge sometimes skips a beat. The displayed value is still correct, because it is computed from startedAt rather than accumulated from ticks. That distinction is the whole design: accumulate time and you inherit every scheduling error; compute from an anchor and drift disappears.
The other limit is granularity. If the UI needs a smooth one-second countdown, the popup runs its own setInterval while it is open and recomputes from the same anchor. The worker never needs sub-minute precision.
For codebases where timer state is spread across handlers, vite-mv3-sw-compile-persist applies the same idea at build time: it marks top-level state dirty, flushes it to storage and rewrites long timers to alarms. It does not persist destructured declarations or anonymous callbacks that close over locals, so the anchor-based model above is still the mental model to keep.
We added a small script to the release checklist: start a session, stop the worker, reopen the popup, and assert that the remaining time is still under the original duration. It runs against a development build before every release. The test is crude, but it would have caught this bug in the first release instead of the third, and it documents the expected behavior for new contributors. It also makes the failure reproducible for anyone reviewing the change.
With and without the plugin
The before/after from the plugin's README is the whole argument:
Without vite-mv3-sw-compile-persist
let count = 0; chrome.action.onClicked.addListener(async () => { const stored = await chrome.storage.session.get('count'); count = (stored.count as number) ?? 0; count++; await chrome.storage.session.set({ count }); }); chrome.alarms.create('cleanup', { delayInMinutes: 5 }); chrome.alarms.onAlarm.addListener((alarm) => { if (alarm.name === 'cleanup') cleanup(); }); With vite-mv3-sw-compile-persist
let count = 0; chrome.action.onClicked.addListener(() => { count++; }); setTimeout(cleanup, 5 * 60_000); The second version is the code you meant to write. The plugin adds restore, dirty tracking, a debounced flush and alarm-backed timers at build time — no runtime import, no wrapper API.
Repository: vite-mv3-sw-compile-persist
If a timer has ever reset on you, one star is a small vote for making this class of bug boring.
What would your extension show if the worker died exactly one second after the user pressed start?
