In his presentation at the Google Chrome Dev Summit in Mountain View CA, Jake Archibald walked through the new ServiceWorker specification that provides offline access to Web experiences. Here are my notes from his talk:
- There are parts of our lives where we are offline. To manage these kinds of interactions, we got AppCache -which is very hard to work with and implement. AppCache looked good on paper, but in practice it was not.
- ServiceWorker is a Javascript context that operates for a set of URLs. Tell it which URLs to control, then point to Javascript file that tells you what to do with those URLs.
- You can listen to network requests, and respond with a URL. You can redirect to other files. This is not an http redirect, but you could set one up if you wanted.
- CahceList can be set up on install. You can tell the cache what to store, then respond to requests with assets in this cache.
- You can create multiple caches and decide which ones to wait on and which ones to respond with when requests come in. You can point to assets on the network or fallback to network fail messages.
- Compared to AppCache, ServiceWorker is more verbose but it gives you complete control of what is happening. It also provides new capabilities and a way to easily debug in the console.
- Most development projects treat offline access as a fallback. This is graceful degradation. It is the wrong way around. Instead we need progressive enhancement: assume people are offline, then enhance if they are actually online.
- With an offline experience, you don't have to wait for the network to render content. This is how many native applications work and they look like the beat Web experiences in performance.
- ServiceWorker levels this playing field by allowing sites to load from cached content first, then go to the network. But we can actually do both things at once.
- Independent control of caching and rooting allows you to implement user controlled caching features like Save For Later.