-
Notifications
You must be signed in to change notification settings - Fork 312
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Detecting service worker termination #1550
Comments
If your ServiceWorker is doing additional processing resulting from a FetchEvent, then you will want to make sure that you are using ExtendableEvent.waitUntil to indicate that processing is ongoing. This is best used with the new (not present in all browsers yet) FetchEvent.handled promise which can let you defer your processing until the response has been sufficiently processed. See #1397 for more details/context on that. If your processing is resource intensive or has a duration that exceeds the normal waitUntil grace periods, then it might be advisable to perform that computation in a dedicated or SharedWorker so you don't impact the responsiveness of the ServiceWorker or trigger browser interventions that mark your ServiceWorker as broken. |
Thank you for your fast reply. I will look into this and verify if I can find a way to use this as a workaround. However, I'm not really sure this is the best way to handle the situation I have in mind, but I could be wrong. With that in mind, a Does that makes sense or do you still think your solution is the best way to go? |
The SW can call both |
Also, it's fine to coordinate the amortized cleanup work using global variables set in the ServiceWorker (protected by an active waitUntil). That is, while it's appropriate to regularly checkpoint state to storage, there's no need to be paranoid about avoiding the use of globals. |
Is there a way to detect when a service worker is about to be suspended/terminated when it's not needed?
Browsers aggressively terminate service workers, in order to save resources.
But what if I wanted to do some computations based on the requests done by the web pages?
I'd like to perform some bookkeeping and save this to IndexedDB. However, at the moment I'm forced to write my computations for every single request to the database. I'd like to batch these writes, in one big write just before the service worker is about to be suspended.
I envision it could be interesting to perform some minor work, such as saving these kinds of computations to the database, by introducing a
beforeTerminate
event. This could be a very limited event in which only small amounts of work would be allowed and after which the service worker would be immediately terminated.Does anyone have any thoughts on this or is there another way to accomplish this?
The text was updated successfully, but these errors were encountered: