[ServiceWorker] requestIdleCallback (equivalent) for ServiceWorkerGlobalScope (#790)

While using service workers, I've come across a few scenarios in which there's "low-priority" work that I'd like to perform. This work, ideally, would be performed at a time that wouldn't block core service worker functionality. Some examples of the type of work I'm talking about:

- Resending one or more queued Google Analytics pings that failed while offline, and were saved in IndexedDB.
- Implementing a cache-expiration policy that clears out older entries based on some configuration.

I have written code to implement both of those scenarios. In first case I run the code whenever the service worker starts up, which introduces a delay before the service worker can handle events. In the second case I run the code inside a `fetch` event handler, after the `event.respondWith()`, which introduces a delay before any subsequent events could be handled.

Something akin to [`requestIdleCallback`](https://w3c.github.io/requestidlecallback/), but available within a service worker context, seems like what I'm thinking about, though that presupposes that the browser would be able to intelligently guess when a service worker is likely to be idle.

Alternatively, this kind of work could be performed in a hypothetical `unload` event, but that's previously been [discussed and rejected](https://github.com/slightlyoff/ServiceWorker/issues/731).

The [Background Sync API](https://github.com/WICG/BackgroundSync) could be used for some types of work, but that feels like it's outside of the intended use caseā€”if every service worker fired off a periodic Background Sync event just to wake up and, e.g., clean out its caches, that sounds inefficient.

Have other folks thought about appropriate eventsto hook into to do this sort of low-priority work? Is there any appetite from the spec maintainers/implementers to add in something like `requestIdleCallback` to the `ServiceWorkerGlobalScope`?

---
Reply to this email directly or view it on GitHub:
https://github.com/slightlyoff/ServiceWorker/issues/790

Received on Monday, 23 November 2015 19:35:33 UTC