UNPKG

3.53 kBMarkdownView Raw
1# Service Worker Toolbox
2
3[![Build Status](https://travis-ci.org/GoogleChrome/sw-toolbox.svg?branch=master)](https://travis-ci.org/GoogleChrome/sw-toolbox) [![Dependency Status](https://david-dm.org/googlechrome/sw-toolbox.svg)](https://david-dm.org/googlechrome/sw-toolbox) [![devDependency Status](https://david-dm.org/googlechrome/sw-toolbox/dev-status.svg)](https://david-dm.org/googlechrome/sw-toolbox#info=devDependencies)
4
5> A collection of tools for [service workers](https://slightlyoff.github.io/ServiceWorker/spec/service_worker/)
6
7Service Worker Toolbox provides some simple helpers for use in creating your own service workers. Specifically, it provides common caching patterns and an [expressive approach](https://googlechrome.github.io/sw-toolbox/docs/master/tutorial-api#expressive-approach) to using those strategies for runtime requests. 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).
8
9## Install
10
11Service Worker Toolbox is available through Bower, npm or direct from GitHub:
12
13`bower install --save sw-toolbox`
14
15`npm install --save sw-toolbox`
16
17`git clone https://github.com/GoogleChrome/sw-toolbox.git`
18
19### Register your service worker
20
21From your registering page, register your service worker in the normal way. For example:
22
23```javascript
24navigator.serviceWorker.register('my-service-worker.js');
25```
26As implemented in Chrome 40 or later, 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.
27
28For even lower friction you can instead include the Service Worker Toolbox companion script in your HTML as shown below. Be aware that this is not customizable. If you need to do anything fancier than registering with a default scope, you'll need to use the standard registration.
29
30```html
31<script src="/path/to/sw-toolbox/companion.js" data-service-worker="my-service-worker.js"></script>
32```
33
34### Add Service Worker Toolbox to your service worker script
35
36In your service worker you just need to use `importScripts` to load Service Worker Toolbox
37
38```javascript
39importScripts('bower_components/sw-toolbox/sw-toolbox.js'); // Update path to match your own setup
40```
41
42### Use the toolbox
43
44To understand how to use the toolbox read the [usage](https://googlechrome.github.io/sw-toolbox/docs/master/tutorial-usage) and [api](https://googlechrome.github.io/sw-toolbox/docs/master/tutorial-api) documentation.
45
46## Support
47
48If you’ve found an error in this library, please file an issue at: https://github.com/GoogleChrome/sw-toolbox/issues.
49
50Patches are encouraged, and may be submitted by forking this project and submitting a pull request through GitHub.
51
52## License
53
54Copyright 2015 Google, Inc.
55
56Licensed under the [Apache License, Version 2.0](LICENSE) (the "License");
57you may not use this file except in compliance with the License. You may
58obtain a copy of the License at
59
60 http://www.apache.org/licenses/LICENSE-2.0
61
62Unless required by applicable law or agreed to in writing, software
63distributed under the License is distributed on an "AS IS" BASIS,
64WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
65See the License for the specific language governing permissions and
66limitations under the License.