Introduction to the Service Workers
20th May 2016
creativcoder / 3min
This post is about a web platform specification, that aims to provide a programmatic control over web pages, by providing a user driven API, which can be used for many purposes i.e., cache data for offline usage, intercept outgoing network requests, provide a platform for implementing push notifcations, and for making a progressive web app.
At the time of writing this, I am working with Mozilla Research on implementing the foundations for the service worker specification in Servo Browser engine as part of my GSoC project (mentored by Josh Matthews). Most major browser vendors already suppport them. Their extent of support can be tracked at caniuse. So, it would be great to have this feature in Servo as well.
So to begin with, lets answer the obvious question:
First, it's a derivative of web workers if you're familiar with them, and shares identity with shared workers. Service workers are event-driven background running scripts (as a seperate thread in Servo and in other browser engines), that run in a time limited manner and can monitor network events made from web pages.
The script's lifetime isn't determined by the the page or the client which registered it, but based on various events that happen during its lifecycle i.e.,
fetch, etc. This behavior is interesting for end users, which means that we should not do do things inside our worker script that tries to persist some data or state. Service Workers should be treated as a
stateless script, which starts and acts only when events occur.
To answer this and to not make this post longer, I would point towards these great Google I/O'16 talks, which really shows their capabilities. The web platform gets a whole new transformation with them when used correctly.
Service Workers at scale
Building offline-first Progressive Web Apps
As said above that service workers basically act as a network proxy listening to network events made from page, so they must be registered first to listen to network events from a page. So jake archibald the co-creator of the service workers specification has a really nice introduction to using service workers today.
The next post will discuss on the initial progress on implementation of Service Workers in Servo. The current implementation does not implement them using promises, as promises support is yet to be integrated in Servo. This will eventually get transitioned to promise based implementation as per the spec standards when promises support arrives. I have a tracking repository, which contains notes and roadmap of Service Workers integration in Servo.
Want to share feedback, or discuss further ideas? Feel free to leave a comment here! Please follow Rust's code of conduct. This comment thread directly maps to a discussion on GitHub, so you can also comment there if you prefer.