Hi!
I run a focus timer extension with close to 60k users. And as you know, with focus timers you deal with time. But the problem I've been facing is that timers are really hard to get right in manifest v3.
Previously when using V2, it was pretty easy since I could just run a setInterval and the background script would do its thing because it was persistent. But in v3, since it's using Workers, it's quite difficult to persist any state and sometimes the service worker dies too.
I migrated to V3 in mid-2023, and I decided to use the chrome alarms API to create my timer. But as you know the timer isn't very accurate and it only works correctly when it's unpacked. To make the timer tick every second, I'm using the "when" argument and creating a new timer every second. This works, but it is hacky and probably consumes more resources compared to the v2 persistent workers.
I also store the countdown state every second so that in case the service worker dies, which it does many times. I can just resume the timer when the worker becomes alive again.
Now here's my problem:
1. My users are not happy with it because the timer is very inaccurate and stalls a lot. I personally use it, too, and it becomes really annoying, so I understand the frustration of my users. Overall, it's not a very good experience.
2. I'm using too many hacks to make it when it worked fine in v2
I've been looking for answers on the web to make it work but there aren't many. I came across a couple of posts but not very satisfying answers or i've tried the solution before.
Then a couple of days ago I came across this from the Docs on how to keep the SW alive.
I'm quite skeptical of this solution since all it's doing is updating the state in local storage. I'm doing something similar with my timer code, which updates local storage each second, but the timer still stops (packed ext) until it's restarted again by some other logic I wrote.
And from the docs
- "We have identified enterprise and education as the biggest use cases, and we specifically allow this there, but we do not support this in general"
- "It is not allowed in other cases and the Chrome extension team reserves the right to take action against those extensions in the future."
These lines are not very encouraging to developers. Do I consider my extension under education or not? What happens when the Chrome team decides my extension doesn't fall under education? Do I just let my extension die?
What I'd like to know is
- Is it recommended to use setInterval in service workers? I know it's not persistent but I have no other option because Chrome Alarms aren't very reliable. setInterval really solves my problem if only the service worker didn't die often.
- What other solutions can I try to get my extension working smoothly
It would be very convenient for all developers to have a persistent flag like we had in v2.
I hope I've explained my problem well. Looking forward to getting a response to this.
Thank you and Have a nice day!