I am writing a Chrome extension for MV3, and am running into a problem where my extension stops working after a while.
Just after manually restarting my extension (and thus its service worker), everything works fine. But after, say, 20 minutes, my extension stops receiving any events I have added a listener for. I have to manually restart either the extension, or the service worker at chrome://serviceworker-internals/?devtools
.
It appears to be because chrome stopped and then reran my service worker after some time. The problem is that this form of rerunning has my service worker not receiving any events.
The following is some minimal code to reproduce the issue:
console.log("Main");
chrome.webRequest.onBeforeRedirect.addListener(
async (details) => {
console.debug(details);
},
{ urls: ["https://*/*"] }
);
chrome.webRequest.onBeforeRequest.addListener(
async (details) => {
console.debug(details);
},
{ urls: ["https://*/*"] },
["requestBody"]
);
chrome.tabs.onUpdated.addListener(
(tabId, changeInfo, tab) => {
console.debug(changeInfo);
}
);
chrome.webNavigation.onDOMContentLoaded.addListener(
(details) => {
console.debug(details);
}
);
When I manually restart, I get the "Main" log and all the debug objects as expected. When I notice my extension stops working and check the service worker's logs, because it has been rerun, the console logs are since cleared, but the only log I see is "Main", implying my listeners did all get readded. Yet no events are received and debug logged. Upon manually restarting, I get the "Main" log and all the debug objects again as expected, until the next time it breaks.
I have seen this SO answer which says that a workaround is to subscribe to webNavigation if webRequest is not working. I am subscribed to both, and my issue is that the service worker is woken up and running, but its events are not firing.