Friday, May 16, 2014

ServiceWorker implementation status in Firefox

ServiceWorkers are a new web platform feature that allows sites to use JavaScript to provide a better offline experience, and eventually various other 'background service' capabilities. Jake Archibald has a good article on his blog explaining them further.

I've been working on the Gecko implementation for a while now, helped by several other Mozillians and some of the first patches are now beginning to land on Nightly. Interest in trying out a build has been high within both Mozilla and outside. I've been procrastinating about a detailed hacks article, but I thought I'd at least share the build and what Firefox supports for now and what it doesn't.

  1. Download the build
  2. Run it using a clean profile.
  3. Go to about:config and set "dom.serviceWorkers.enabled" to true.

  • Registration, installation, activation, unregistration. Update() is not according to the spec, the scripts themselves are not cached offline, so don't actually try to make offline apps yet.
  • Intercepting navigation and fetch events and replying using a simple SameOriginResponse object.
  • Intercepting Push API events.
Not supported:
  • Cache API
  • fetch() - You'll have to use XHR in the worker.
  • ClientLists and postMessage() to talk to windows
  • Persistence - ServiceWorker registrations are currently not stored across restarts.
  • Anything that throws an error :)
  • No devtools support right now.

SameOriginResponse is a relic of an earlier version of the spec. Similarly the Request object received from the fetch event is not exactly up to speed with the spec.

I have two sample applications on Github to try out the build. Note that ServiceWorkers require HTTPS connections. For development, please create a new boolean preference in about:config - "dom.serviceWorkers.testing.enabled" and set it to true. This will disable the HTTPS requirement.

Push API Test - Remember to run the Firefox build from the command line since this demo prints stuff to the terminal. Click the register button to sign up for Push notifications. To perform the push, copy the unique URL and run

curl -vX PUT 'URL'

The ServiceWorker will be spun up and a 'push' notification delivered.

Simple Network Interception - This example will load an iframe with some content. Refresh the page and the iframe's content should change and the browser should show an alert dialog. These changes are done by the ServiceWorkers (there are 2, one for the current scope and one for the sub/ scope) intercepting the requests for both "fakescript.js" and the iframe and injecting their own responses.


We could always use help with the specification, implementation and testing.

The specification is improved by filing issues.

The tracking bug for the Gecko implementation is Bug 903441. There is a mercurial patch queue for patches that have not landed on trunk.

I (nsm) am around on the #content channel in if you have questions.

1 comment:

  1. Wow, just this morning I was thinking, in my own muddled way, that something like this was essential if web apps were to compete with native apps on mobile.

    For example, I use to generate random numbers for myself on my mobile sometimes. I use it intentionally, rather than a native app, to test the pure-web experience for something simple like this. And I find it works --- it works really well, in fact, despite the page not being mobile-optimised --- but the only annoying thing is the delay in bringing up the page in the first place.

    ServiceWorkers will solve that. The only issue is that it looks like it will still need a bit of care and effort to do properly and it requires HTTPS, both of which are small-site unfriendly and will additionally stymie adoption. (Though I understand the desire for HTTPS in this case.)