Skip to main content

New! Service Worker For Offline Viewing

Posted on: 08/23/2017 · Estimated reading time: 2 minutes to read

If you’re viewing my site on an Android device, you may notice a popup inviting you to install “Matthew Graybosch” on your device. Don’t panic! It’s just the new service worker that I’ve added.

WTF is a service worker?

As Matt Gaunt writes in Google’s “Service Workers: an Introduction”:

A service worker is a script that your browser runs in the background, separate from a web page, opening the door to features that don’t need a web page or user interaction. Today, they already include features like push notifications and background sync. In the future, service workers will support other things like periodic sync or geofencing.

I’m not using mine for anything so fancy. Instead, my service worker just grabs all of my posts and images in the background so that they’re ready when you want them. If you have a Jekyll-based blog you can do the same using this FOSSBYTES article.

Do I really need the service worker?

My site should work even if you go into your browser settings and disable JavaScript outright, or use add-ons like NoScript to deny JS functionality to my site. More importantly, all of the code for my website is publicly available on GitHub, so if you have the necessary skills you can see for yourself that my stuff’s on the level.

Installing my website on your device is entirely optional, but it allows you to read my blog even if you’re offline. You could access my site at home while connected to your wifi network, and then read articles on the go without using mobile data.

Why did you do this?

I did it because I could. I also did it because getting the service worker working properly on this site will set the stage for me to use it on the new Starbreaker site I plan on building at starbreakersaga.com, where you’ll be able to read rough cuts of my novels in their entirety.

If you run into any problems or have any questions, please email me so I can fix it ASAP.

Update

The one kink I had with the service worker was interference from Cloudflare. I just figured out a fix and implememented it.

I’ve been taking advantage of Dreamhost’s free basic Cloudflare service to ensure that my site gets served from a CDN and is thus better defended against DDOS attacks. The downside is that whenever I changed the stylesheet, I had to log into my Cloudflare account and purge their cache.

This got annoying, so I dug into Cloudflare’s application programming interface (API) and found a way to script the purge operation. It requires the Unix tool curl, as well as the account email, API key, and zone ID for authentication.

curl -X "DELETE" https://api.cloudflare.com/client/v4/zones/${CLOUDFLARE_ZONE_ID}/purge_cache \
-H "X-Auth-Email: ${CLOUDFLARE_USER_EMAIL}" \
-H "X-Auth-Key: ${CLOUDFLARE_API_KEY}" \
-H "Content-Type: application/json" \
     --data '{"purge_everything":true}'

With this running at the end of my deploy script, every time I change the style sheet or upload a new post, Travis will automatically purge Cloudflare’s cache after deploying to my host. Travis also securely stores values for the variables in my command, so I can safely expose this code in GitHub.

Now the service worker should work perfectly as long as I remember to update the version number as needed.

Update for 28 August 2017

I’ve ripped out the service worker. Even when bumping version numbers, changes like new posts and style updates don’t make it through. I might try this again later, but for now it’s more trouble than it’s worth.