Polytechnic

The personal brain-dump of Garrett Coakley

Going Offline

Over the last couple of weeks I’ve been reading Jeremy’s excellent book Going Offline. It’s a step by step exploration of Service Workers. If you’re not sure what a Service Worker is the Mozilla Developer Network has a series of articles to get you up to speed:

Service workers essentially act as proxy servers that sit between web applications, the browser, and the network (when available). They are intended, among other things, to enable the creation of effective offline experiences, intercept network requests and take appropriate action based on whether the network is available, and update assets residing on the server. They will also allow access to push notifications and background sync APIs.

I already had a service worker on this site that had been built with Google’s Workbox library, but it always felt a little like “magic” to me. I wasn’t entirely sure what was going on under the hood, and I only ever managed to get it to do some caching of static assets, nothing more interesting than that. It felt like I was learning how to use a library, not the underlying tool.

That all changed with Going Offline. With his typical self-effacing humour (chapter titles include Making Fetch Happen and Cache Me If You Can), and easy manner, Jeremy explains how Service Workers, uh, work, the clever things you can do with them, and most importantly, how to build your own. Not only that, but it also acts as a great introduction to asynchronous JavaScript, something I’ve had real problems getting my head around. I’m still a long way from being proficient, but thanks to this book, I feel like I “get” it now.

To that end, this site now has its own home-grown, organic, corn fed, Service Worker.

As well as the caching of static assets it now cleans up after itself by deleting old caches, and more importantly will serve an off-line page if the user’s connection goes down. That page isn’t anything special right now, but one of the techniques that Jeremy talks about is caching pages that the user has visited, so at least they can load something from the past. One for me to think about.

I highly recommend picking up a copy of Going Offline, it’s a brilliant read and a great introduction into the power of Service Workers.

Responses and comments