- HTTPS Required - Service Workers are restricted to running across HTTPS for security reasons.
- Same Origin Required - Service workers must be from the same domain as for the page/app they are registered.
- Beware Global Variables - A single service worker can control many pages. Each time a page within your scope is loaded, the service worker is installed against that page and operates on it. Bear in mind therefore that you need to be careful with global variables in the service worker script: each page doesn’t get its own unique worker.
Service worker functions like a proxy server, allowing you to modify requests and responses, replace them with items from its own cache, and more.
- install - start populating indexeddb and cache site assets
- activate - Cleanup resources used in previous versions of service worker script. It is now safe to do stuff that would have broken the previous versions while they were running.
- fetch - retrieve resource managed by the service worker
// You can call this method unconditionally from the controlled page,
// i.e., you don't need to first check whether there's an active registration.
ServiceWorkerContainer.register(scriptURL, options).then(function(ServiceWorkerRegistration) {
// Do something with ServiceWorkerRegistration.
});
You are also responsible for periodically purging cache entries. Each browser has a hard limit on the amount of cache storage that a given service worker can use. The browser does its best to manage disk space, but it may delete the Cache storage for an origin. The browser will generally delete all of the data for an origin or none of the data for an origin. Make sure to version caches by name and use the caches only from the version of the ServiceWorker that they can safely operate on.
Chrome has chrome://inspect/#service-workers, which shows current service worker activity and storage on a device, and chrome://serviceworker-internals, which shows more detail and allows you to start/stop/debug the worker process.