What if a Service Worker could allow a web site/web app to expose a web
service to other apps, even while offline?
Currently we have several not very web-like proprietary JavaScript APIs in
Gecko for sharing data between apps in Firefox OS, like the Contacts API,
DataStore API, Settings API and Device Storage API.
It seems that a more webby alternative for these use cases could be for a
hosted web service (e.g. tied to your Firefox Account) to handle the
storage of data like contacts and photos, accessed by web apps via a REST
API. Currently the problem with that is that REST APIs are not available
while offline.
The misleadingly named "navigator.connect" API [1] being proposed by Google
for offline cross-origin communication via Service Workers actually
includes not only cross-origin postMessage, but cross-origin handling of
onfetch events. The example use cases given include a font service which
could provide fonts to multiple origins while offline using a Service
Worker. I wonder whether this could be extended to allow JSON to be
fetched from another origin over XHR/fetch while offline?
Imagine a gallery app hosted at
gallery.firefoxos.com which stores your
photos online, but caches them locally as blobs in IndexedDB. A camera app
at
camera.firefoxos.com could use a REST API at
gallery.firefoxos.com to
save photos to your gallery, and to get thumbnails of recently taken
photos. If
gallery.firefoxos.com had a Service Worker registered to handle
onfetch events originating in a fetch request from
camera.firefoxos.com
then this could also work offline.
A web page at
dialer.firefoxos.com could send an HTTP request:
GET
http://contacts.firefoxos.com/{contactId}
Accept: application/json
which is intercepted by a Service Worker which provides a response even
when offline.
Being "cacheable" and a "layered system" where "the client can not tell
whether it is connected directly to the end server or an intermediary along
the way" are key principles of a RESTful architecture [2]. Service Workers
just make this far more powerful by making the cache programmable!
Could this even allow us to expose APIs using common standards on the web
like CalDAV, CardDAV and OpenSearch while offline?
contacts.firefoxos.com
could provide an OpenSearch API for the Rocketbar to search the user's
contacts for example, as could any other app.
CORS allows web sites to provide cross-origin web services over HTTP, how
could this work with Service Workers?
I've filed an issue on GitHub [3] for the navigation.connect specification
to propose this as a potential use case, and I'm interested to know what
you think of this idea?
Ben
1.
https://github.com/mkruisselbrink/navigator-connect
2.
http://en.wikipedia.org/wiki/Representational_state_transfer
3.
https://github.com/mkruisselbrink/navigator-connect/issues/23
<
http://en.wikipedia.org/wiki/Representational_state_transfer>