Shed
A collection of tools for Workers
Service Worker helpers
Shed provides some simple helpers for use in creating your own service workers. If you're not sure what service workers are or what they are for, start with the explainer doc.
Registering your service worker
From your registering page, register your service worker in the normal way. For example:
navigatorserviceWorker;
For even lower friction, if you don't intend to doing anything more fancy than just registering with a default scope, you can instead include the Shed companion script in your HTML:
As currently implemented in Chrome 40, a service worker must exist at the root of the scope that you intend it to control, or higher. So if you want all of the pages under /myapp/
to be controlled by the worker, the worker script itself must be served from either /
or /myapp/
. The default scope is the containing path of the service worker script.
Using Shed in you worker script
In your service worker you just need to use importScripts
to load Shed
; // Update path to match your own setup
API
// shed.router has get, put, post, delete, head and any methods, matching HTTP// verbs. It uses ExpressJS style route syntaxshedrouter; // The built-in handlers are networkFirst, networkOnly, cacheFirst, cacheOnly// and fastest. The networkFirst, cacheFirst and fastest handlers will update// the cached version if a response is fetched from the network.shedrouter; // Try the network, fallback to cacheshedrouter; // Try the network, fail if not availableshedrouter; // Try the cache, fallback to network if not in the cacheshedrouter; // Try the cache, fail if not cachedshedrouter; // Request from the cache and the network, return which ever comes back first // You can use relative URLs, which are relative to the scope of the workershedrouter; // For requests to other origins, pass the origin in the parameters to the routeshedrouter; // Origins can be specified as a RegExpshedrouter; // At the moment, if the fetch is for a URL that doesn't match a route, you get// some default behaviour. The 'default default' is networkOnly, but you can// change it like so:shedrouterdefault = shedcacheFirst; // If you want some resources to be cached during the install event, specify// them with the precache methodshed; // You can manually add or remove things from the cacheshed;shed; // You can pass options to most methods// The `debug` option turns on verbose console loggingshedrouter; // The `cache` option tells shed to use an alternative cache. Provide the string name of the cache, not a Cache object.shed; // These can also be set globallyshedoptionsdebug = true;shedoptionscache = 'main cache';