My Chrome Extension has alarms permission.
During my testing, I set up an alarm in my service-worker (background script) to fetch some data every 10 min. It works as expected.
For the release, I changed my alarm to run every 3 hours. Unfortunately, it does not appear to run every time. (only few users had the alarm run and it was reported they did not close their browsers). I was puzzled why.
After doing a bit of research, I found out that service workers can be unloaded by Chrome as part of Chrome's performance optimizations.
- I thought that alarms should cause service to be loaded again. It seems like this is not true.
- Others here suggested to implement some functionality to always keep worker up (either by pinging it or having it send HTTP requests, etc). But this does not seem like a good solution.
- If there is no way to wake service worker up, I want to implement a functionality to run missed alarms whenever it is loaded back up (whenever users would interact with extension). Is this the best approach or I am missing some fundamentals.
My service-worker.js:
createAlarms() // calls chrome.alarms.create and chrome.alarms.onAlarm.addListener
chrome.runtime.onStartup.addListener(async () => {
runMissedAlarms() // get alarm execution times from chrome.storage.local and determine if it was missed
});