Service worker: Important to PWAs

Service worker: “Essentially a JavaScript file that runs separately from the main browser thread, intercepting network requests, caching or retrieving resources from the cache, and delivering push messages.” Service workers also “provide offline access to cached content.” (Source)

A web worker is “an object created using a constructor (e.g. Worker()) that runs a named JavaScript file — this file contains the code that will run in the worker thread; workers run in another global context that is different from the current window.” (Source)

A service worker is a type of web worker.

How about browser support for service workers? Check here.

A service worker can allow your app to show push notifications even when the app is not open in the browser. This is one of the hallmarks of progressive web apps (PWAs). What about when the browser itself is not open? Android will “wake up any browser when a push message is received” (source), but other operating systems (currently) will not.

“To communicate with the page, the service worker uses the postMessage() method to send data and a ‘message’ event listener to receive data.” (Same source.)

More (from the same source):

  • Service workers only run over HTTPS.
  • Service workers make extensive use of promises.
  • For making an app work offline, service workers depend on these two APIs: Fetch and Cache.
  • When you register a service worker (in your script), you can explicitly set its scope. (“The default scope is the location of the service worker file, and extends to all directories below.”)
  • After the service worker is registered, it is installed, and after that, it is activated. However, activation only occurs after all pages still using the old service worker have been closed. You can never have more than one version of the service worker running concurrently.
  • Use self.addEventListener() to detect 'install' and 'activate' events.
css.php