how to keep service workers alive longer?

101 views
Skip to first unread message

Tyler Crews

unread,
Oct 7, 2021, 12:47:47 PM10/7/21
to Chromium Extensions
I'm having an issue with my extension -

one of the features that I'm trying to implement is a badge that counts down with a setTimeout timer, from a given amount of time down to 0 when the badge will clear out.

But I've been running into several issues, one of which being that the service worker will sometimes go inactive, killing the settimeout function and stopping the clock in the middle of the badge.
I've only just noticed because I guess while I hold the service worker inspect view open it may be keeping the worker alive longer than it does out in the wild, so to speak?
If I don't keep the inspect view open it often just dies 10s later, so even trying to set an alarm with a period of 1 minute isn't working for me.

So is there an optimal way to maintain the service worker's existence long enough to do a countdown effectively?

wOxxOm

unread,
Oct 8, 2021, 10:25:41 AM10/8/21
to Chromium Extensions, tycr...@gmail.com
This is yet another use case completely disregarded and broken by ManifestV3.

There are several workarounds, see https://stackoverflow.com/a/66618269. In short: for timer durations shorter than five minutes (300 seconds) you can use the built-in SW lifecycle management via its `activate` event, otherwise use runtime ports; when the countdown is finished you will allow the worker to terminate as shown in the link.

Tyler Crews

unread,
Oct 8, 2021, 1:26:26 PM10/8/21
to Chromium Extensions, wOxxOm, Tyler Crews
damn, I was worried that would be the case. Kind of a bummer but so it goes.

thanks!

Reply all
Reply to author
Forward
0 new messages