Skip to content

Commit

Permalink
A collection of tools for Workers
Browse files Browse the repository at this point in the history
  • Loading branch information
wibblymat committed Dec 17, 2014
1 parent e46ac9d commit d448578
Show file tree
Hide file tree
Showing 10 changed files with 930 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
npm-debug.log
24 changes: 24 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"bitwise": true,
"camelcase": true,
"curly": true,
"eqeqeq": true,
"forin": true,
"immed": true,
"indent": 2,
"latedef": true,
"newcap": true,
"noarg": true,
"noempty": true,
"nonew": true,
"plusplus": false,
"quotmark": "single",
"undef": true,
"unused": "vars",
"strict": true,

"worker": true,
"devel": true,
"node": true,
"predef": ["fetch", "Request", "Response", "caches", "self", "URL"]
}
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ Apache License
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright {yyyy} {name of copyright owner}
Copyright 2014 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# 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](https://github.com/slightlyoff/ServiceWorker/blob/master/explainer.md).

From your registering page, register your service worker in the normal way:

```javascript
navigator.serviceWorker.register('/service-worker.js', {scope: '/'});
```

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/`.

In your service worker you just need to use `importScripts` to load Shed

```javascript
importScripts('../shed/dist/shed.js');
```

## API

```javascript
// shed.router has get, put, post, delete, head and any methods, matching HTTP
// verbs. It uses ExpressJS style route syntax, extended to allow absolute URLs
// on other domains
shed.router.get('http://example.com/:page', function(request, keys) {
return new Response('Handled a request for http://example.com/' + keys.page);
});

// The built-in handlers are networkFirst, networkOnly, cacheFirst, cacheOnly
// and fastest
shed.router.get('/shed-demo/:foo/:bar', shed.networkOnly);

// You can use relative URLs, which are relative to the scope of the worker
shed.router.get(':foo/:bar', shed.networkOnly);

// 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 netowrkOnly, but you can
// change it like so:
shed.router.default = shed.cacheFirst;

// If you want some resources to be cached during the install event, specify
// them with the precache method
shed.precache(['/index.html', '/site.css', '/images/logo.png']);

// You can manually add or remove things from the cache
shed.cache('/data/2014/posts.json');
shed.uncache('/data/2013/posts.json');
```
1 change: 1 addition & 0 deletions build/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
self.shed = require('../lib/shed.js');
Loading

0 comments on commit d448578

Please sign in to comment.