When an MV3 service worker goes quiet.
Manifest V3 background code runs in an event-driven service worker. It can stop and restart, so a global variable is not durable state. Debug the lifecycle and the data boundary together.
1. Inspect the worker that is actually loaded
Open chrome://extensions, enable Developer mode, find the extension, and open its service worker inspection link. Reproduce one action while watching the console. Confirm the loaded manifest points at the file you edited and that the worker has not stopped on an earlier exception.
After a code change, reload the extension and close stale DevTools windows. A background console from an old version can make a correct fix look ineffective.
2. Treat globals as a cache, not a database
Because the worker may restart, read durable state from chrome.storage inside the event that needs it. Validate the result before dereferencing it and return a useful response when setup has not happened yet.
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message?.type !== 'OPEN_PROJECT') return;
chrome.storage.local.get('activeProject').then(({ activeProject }) => {
if (!activeProject?.id) {
sendResponse({ ok: false, code: 'PROJECT_NOT_SELECTED' });
return;
}
sendResponse({ ok: true, id: activeProject.id });
});
return true; // keep the channel open for the async response
});3. Check permissions and message contracts
- Verify every API used by the worker is available under the extension’s declared permissions.
- Validate message type and payload before doing work; treat messages from tabs as untrusted input.
- Handle rejected promises and return an explicit error instead of leaving the popup waiting forever.
- Test a fresh install, a reload, a worker restart, and cleared storage.
Have the manifest, console error, and message handler?
Paste the smallest relevant pieces for a focused repair packet. DebugSmith never executes an extension or changes your files.
Run a free scanFurther reading: Chrome extension service workers and Chrome extension messaging.