Service workers for your PWA (Progressive Web App) website

Jul 12, 2019

Two pillars for transforming your website into a PWA (Progressive Web App) are the web app manifest and service workers. (Besides that, it’s better if your website is responsive and follows PRPL principles, but I won’t go into those in this blog post.) A manifest is kind of a no-brainer, although it requires some work. You need to identify the main theme color and background color for your website, generate various sizes of the favicon, and reference some of those from the manifest and some from the header meta section. There are several websites (Example) that help you generate a series of images and the corresponding manifest and HTML meta tags.

On the other hand, service workers are not as straightforward, since your website could take several shapes and forms (for example, SPA / non-_SPA_), sub-URLs, and assets. You first need to decide what caching strategies you prefer and for what part of your website: do you prefer an offline-first or online-first approach? An online-first approach could be a good idea if your website, for example, displays cryptocurrency tickers and their current values. In the case of a static website with a blog section (like mine), you are good to go with an offline-first approach. Still, you need to configure exactly which files the service worker infrastructure should cache for offline use, and in what way (expiration, etc.). You can decide the approach on a per-resource basis, so your static website can be offline-first, while only the trade ticker part could be online-first with an offline fallback.

In the past, Google provided libraries like sw-precache to help with this configuration task and possibly cut down on plumbing code related to the service worker configuration and activation mechanisms. The newest generation of Google’s service worker helper library is Workbox, which takes help to the next level; I believe we have arrived at a point where the configuration is minimal and painless. Ideally, you only want to specify the list of assets (possibly with wildcards) and what strategy you want to follow for each. Then the framework should take care of the rest for you.

Along those lines: if you are using any static site generator framework, then there’s a very good chance that your framework has a package that provides a layer on top of Workbox, and you can deal with it at the level of your generator framework instead of Workbox’s JavaScript API. A common principle in software engineering is to avoid reinventing the wheel, so you can spend your time on more meaningful tasks for your project. Unless your framework of choice is very exotic and doesn’t have much community behind it yet, service worker configuration and dealing with Workbox must have been done by thousands of engineers before you, and steps have probably been taken to make this part seamless. This means: always look into how your selected framework supports PWAs and look for a layer on top of Workbox.

At the time of this blog post, this site uses Jekyll, and I saw two candidates (BTW, multiple choices are usually a good sign of a thriving community): jekyll-pwa-plugin or jekyll-workbox-plugin. Both of them seem nice, but based on this blog post, I decided to go with the latter. Since Jekyll is a RoR (Ruby on Rails)-based framework, the "package" you need to install is a Ruby Gem. The configuration is simple, easily readable (YAML format) with no fluff inside the expected _config.yml, and requires just a few plumbing code placeholders. I only needed to add two JavaScript entries:

In default.html: source

if ('serviceWorker' in navigator) {
  window.addEventListener('load', function() {
    navigator.serviceWorker.register("/sw.js").then(function() {
      console.log("Service Worker Registered");
    });
  });

And a short sw.js file stub (source)

// let Workbox handle our precache list
// NOTE: This will be populated by jekyll-workbox-plugin.
workbox.precaching.precacheAndRoute([]);

And that’s all!

Comments loading...