Transparent caching with Service Workers
Service Workers are becoming one of the most effective ways to progressively enhance web applications. Its main purpose is to enable offline experiences, allowing to provide web notifications and running background tasks even if the web page is not active. In this brief article we comment how we applied it to implement and manage resource caching in a transparent way for an existing web application.
Web and Service Workers
Service Workers are a special kind of Workers that can enhance web applications, making them run faster and be more engaging. As we commented before, in this case we will be focusing on asset and request caching as a part of a requirement for one of our applications. First of all, in order to use service workers we need to create our service worker file. This file should be served from the same domain as our webpage and from the root path to provide a wider scope. The first thing we have to do is to check if the service worker API is available and proceed to register it.
Adding caching and installing
Having our Service Worker already set up, we start implementing the caching piece. In this case, all the caching will occur in our Service Worker file, being this process completely independent from the web page. The most basic Service Worker consists of listening on the two main lifecycle events of it,
install event occurs only once per Service Worker and, if it has happened already, it won’t be triggered in the next page load. Every time we change the code in the Service Worker, the browser is going to detect as a new one and install it again. This is a good place to cache our assets.
In a Service Worker environment, as well in the browser the,
caches object provides a mechanism for caching
Response objects. Here we will open the current cache and add all the files we want to cache. To do so, we will create a key representing the request and with the value of the response. In the activate event, in this case we make sure that cleaning all other caches and leaving the one that is of our interest.
Serving the cached resources
Once the Service Worker is active, we can start listening to the
fetch event and snoop all requests that go through our domain, and then we can choose to continue with the request or respond with things that are in our cache. After this, the webpage will work offline without any further effort or invasive code to be added.
And this is a short example…
Service Workers are quite powerful, from here it is possible to start creating more complex things like using a stale-while-revalidate approach where we serve things from cache first, and update everything as soon as we get a response from our servers. This is an approach we used at caches in several layers of our apps, even in our backend, and having it in a web fronted is now doable in a transparent way using Service Workers too.
Finally, there is a tool from Google that can help creating powerful service workers, it is called Workbox, more information at https://developers.google.com/web/tools/workbox.
If you are interested in this topic, here is more articles that you may want to look at: