So, the first thing we're doing in here is we're creating a variable called counter and we're signing it to zero. Next, we're assigning an on install method on our self object and that tells us when the service worker is being installed. So, in this case, we're just going to log a message that the install happened.
After that, we have an on activate, and so this tells this gets called when the service worker reaches the activation stage or state, as it were, these service workers act as a state machine. So, this happens, we're just going to log a message that the service worker is now active, but then we're also going to do something else that's kind of interesting. When a service worker is first activated, any pages that are open, requests that they send will not immediately get sent through this service worker. It isn't until the page is refreshed that those pages are then under control of the service worker. So, what we're doing here at the bottom, we're calling event.waitUntil, which accepts a promise, and the promise is the result of the self.clients.claim call. So, that's saying basically the pages that are currently open, the service worker will intercept the request. All right.
So, the file continues, and here's the second half. We also have this on fetch handler. This on fetch handler, this gets called every time the service worker receives a network request from one of the web pages. So, inside of this one, what we're doing is first logging the URL that was received. Next, we have an if statement, so in here, we check to see if the URL that was received ends in data.json. If it does not, then we skip to the end and we fall back essentially to a normal HTTP request. So, in here, we have this event that respond with, and that accepts a promise which resolves into a value that is given to the browser as a result of the request. And so, the value that we're giving it is then the response from fetch, which is a promise, and then the argument that we're providing to fetch is the incoming request, which is event.request. So, essentially, what that line is saying is just perform a normal request and don't really do anything with it. However, if the URL did end in data.JSON, we execute the body of that if statement. So, what we do in there is we increment that counter variable from before, and then we just call event.respondWith, you can ignore that return void, but we just call the event.respondWith, where we pass in a new response that we're creating from scratch. And so, the body of that response is a JSON object that contains our counter in the variable. And then, just for illustrative purposes, muscle setting the headers, so in this case, we're setting the content type to text.JSON. And so, if we actually execute this code, this is the logs that we'll see. So, the first thing we see is that the service worker is installed. Next, we see that the service worker has been activated. And then, finally, we see that the controller change has happened. Again, sort of the order of these messages is non-deterministic, as the browser, you know, may handle a log and buffer it in memory. Well, it will print a log from another location. So, the order of this, those first two messages, is not guaranteed.
Comments