Feed readers are more useful when they work offline. Unfortunately, up until a couple months ago, the web’s technologies were not compatible with this requirement. Yet, the introduction of Service Workers is slowly changing this game and, at Superfeedr, we’re excited to see open web technologies catch up with capabilities of installable native apps.
We have our very own RSS reader built on top of Superfeedr’s API: river.news. Let’s see what Service Workers can do for it and improve our code. As a reminder, this is a static single page application: there’s no application server which computes a different response for each request. Check the source code on Github.
Adding support to river.news would be very interesting on 2 levels:
- Since the code is static, there’s no need to load it from the file server after its first load. SubToMe used AppCache for this…. but AppCache is so messy.
- When loading past stories from Superfeedr, we should keep them in the cache so that the user can read them even when they go offline or have a spotty connection.
An application shell
The application’s shell must immediately be cached the first time the page is loaded and we should only invalidate the cache for the files that have changed (incremental updates are much better than native apps!). Even though the Service Worker API is fairly simple, the Chrome team provides some syntactic sugar in the form of
sw-precache. This script lets anyone quickly define the files required for the shell and handles invalidation without any significant code change to your own code!
That was the easy part. Next week, we’ll see how we can also cache the API calls to Superfeedr for offline usage!