The second big thing is the Service Worker. So first of all, my app is a JavaScript application, so there's a lot of application logic written in JavaScript, which is cool because I really like JavaScript. Then again, as soon as we close the site, as soon as we close the browser, there's just, JavaScript is just dead. There are no things happening inside our app.js anymore, so we can't interact with it anymore.
The Service Worker is now also a piece of JavaScript, but it lives in a separate scope. It's registered between the web app and the browser, and there it is able to listen to events, to interact with the site, even if the browser is closed, so it stays there forever. It's not bound to the lifecycle of our application.
To register the Service Worker, we can call this navigator.serviceworker.register function, points to the Service Worker. It has some optional parameters like the scope, so in our case, our Service Worker controls everything, so our site and all the subfolders as well. We can also create a new Service Worker that controls specific scopes inside our application, so we could have the About Service Worker that only controls everything inside this About folder, for example.
And then, inside the Service Worker, it's just JavaScript. We do have event listeners, so whenever someone visits the page, this Install Event is fired. Then once the installation happened, we have the Activate Event. Then every request goes through the Service Worker with the Fetch Event, and also if someone tries to reach our Service Worker from some server, for example, we do have the Push Event that we can listen to. So far, so good.
But let's get back to our super web app and its superpowers. Let's start with issue number one, Super Web App and the dream of network independence. It's a beautiful day, sun is shining, our super web app tries to request some assets from the server, it makes a request to the server, comes back with the response, everything was fine. But suddenly, the internet connection is gone. Sorry, there we go. Different. The internet connection is gone, and well, Donasaur appears, certain death for all classic web applications. Not so for our super web app, because we do have the Service Worker superpower.
Now the Service Worker sits between the website and the internet, and it can also interact with the application storage. So even if the internet connection is gone, we still have a reliable source to serve our request. How can we use it? So in this case, we have our SVG, we have a fallback SVG image and we want to show the fallback SVG whenever a request to an SVG image fails. First of all, in the install event, we opened the cache, we will then put this fallback SVG into the cache. Then once we have the fetch event, we will first check whether the requested asset is actually an SVG. If that is the case, we do have the event.responseWith, which basically takes over the request. There we can then pass the request to the server.
Comments