Menu

1 / 2

Write Your Chrome MV3 Background Worker Like It Never Dies

DEV Community: javascript·Dmitry·7 days ago
#5rJxAV9o
#devtojavascript#devto#dev#chrome#every
Reading 0:00
15s threshold

How a build-time AST transform finally makes Manifest V3 state persistence painless

Manifest V3 didn’t just change the extension model — it actively punishes normal JavaScript patterns.

Your background service worker is killed after roughly 30 seconds of inactivity. When Chrome restarts it, every top-level let , every Map , every pending setTimeout is gone. The next event handler starts from a clean slate.

The official workaround is brutal:

Manually serialize every piece of state to chrome.storage

await it back on every event

Rewrite long-running timers as chrome.alarms

Maintain careful locking so concurrent restarts don’t cause duplicate work

The result is hundreds of lines of defensive plumbing that has nothing to do with your actual feature.

A different approach: transform the code, not the developer

vite-mv3-sw-compile-persist takes a build-time approach. You keep writing ordinary JavaScript.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.