What is a service worker?
A service worker is a bit of JavaScript that sits between our application and the network.
You can think of a script that runs outside the context of our application, but with which we can communicate from within the bounds of our code. It's a piece of our application, but separate from the rest.
The easiest example is in the context of caching files (which we’ll explore in the upcoming chapters). Let’s say that our application, when the user navigates to https://chatastrophe.com, goes and fetches our icon.png
file.
A service worker, if we configure it, will sit between our app and the network. When our app requests the icon file, the service worker intercepts that request and checks the local cache for the file. If found, it returns it; no network request is made. Only if it doesn't find the file in the cache does it let the network request go through; after the download is complete, it puts the file in the cache.
You can see where the term "worker" comes from--our...