I'm using this solution by wOxxOm here to keep the service worker alive in my chrome extension (manifest v3).
I used the offscreen solution because I use win7 so I can only use chrome v109. it works very well, but when the PC hibernate then wake up, the service worker will restart with a probability of 50%. for example if I hibernate after 10s and wake up after 10s then the SW will restart always...
This problem does not happen in my tests when I use background scripts using manifest v2 with "persistent" : true
Is there something I can do to make the SW persistent (not restart after hibernation) as in MV2?
I tested other solutions but none worked, for example I discovered that I can make the SW persistent using alarms too:
// keep SW alive for 30s
// self.serviceWorker.postMessage('test')
// this will keep SW alive for 5min then chrome will kill it, to prevent this kill we will run alarm every 4min
// we run setInterval every 20s to prevent SW sleep in the first minute
setInterval(()=>{self.serviceWorker.postMessage('test');console.log("___________postMessage every 20 seconds");},20000);
chrome.runtime.onInstalled.addListener(() => {
chrome.alarms.create('keepAlive', { periodInMinutes: 4 });
});
chrome.alarms.onAlarm.addListener((info) => {
if (info.name === 'keepAlive') {
self.serviceWorker.postMessage('test');
console.log("++++++++++++alarm every 4 min");
}
});
But it did not solve the hibernation problem too. I'm out of ideas, any idea please?
is this problem solved in newer chrome versions? i have only win7 i cannot test the newer chrome versions unfortunately.