https://github.com/sdgluck/sw-register
:dart: Service Worker registration utility
https://github.com/sdgluck/sw-register
Last synced: 3 months ago
JSON representation
:dart: Service Worker registration utility
- Host: GitHub
- URL: https://github.com/sdgluck/sw-register
- Owner: sdgluck
- License: mit
- Created: 2016-06-10T08:33:28.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2016-07-24T09:44:49.000Z (almost 10 years ago)
- Last Synced: 2025-10-08T21:24:09.370Z (8 months ago)
- Language: JavaScript
- Homepage:
- Size: 15.6 KB
- Stars: 11
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# sw-register
> Service Worker registration utility
Made with ❤ at [@outlandish](http://www.twitter.com/outlandish)
[](http://standardjs.com/)
Register or retrieve a Service Worker that controls the page with ease...
- Register a Service Worker with one call to `register()`
- Get an existing Service Worker with one call to `register()`
- Always favours returning the active Service Worker over registering a new one, which means you can put a
call to `register()` with options in your init procedure and it will not try to re-register the worker.
Saving you having to inspect `navigator.serviceWorker.ready` manually, etc.
## Install
```sh
npm install --save sw-register
```
## Import
```js
// ES6
import register from 'sw-register'
// CommonJS
var register = require('sw-register')
// RequireJS
define(['sw-register'], function (register) {/*...*/})
```
```html
```
## Register
```js
const register = require('sw-register')
// Get existing worker
register().then((worker) => {
const channel = new MessageChannel()
worker.postMessage("Hey!", [channel.port2])
})
// Register a worker from the client
// Worker scope depends on worker location/headers
// Read the Service Worker docs for more details
const registration = {
url: '/sw.js',
scope: '/'
}
// Subsequent calls with a registration object
// will not attempt re-registration but return
// the existing registered worker instead
register(registration).then((worker) => {
const channel = new MessageChannel()
worker.postMessage("Hey!", [channel.ports2])
})
// Catch registration error
register().catch((err) => {
console.log(err.message)
})
```
## Usage
`register([options]) : Promise`
Register or retrieve a Service Worker.
- [__register__] {Object} Options object
Returns the Service Worker controller.
Available options (any not listed can also be passed in):
- `url` (required) the URL of the service worker
- `scope` (optional) the scope of the service worker
- `forceUpdate` (optional) call `update()` on the registration?
Example:
```js
register().then((worker) => {
// do something with the worker
}).catch((err) => {
// you get here if:
// - service workers unsupported
// - no existing worker and no options given to register one
})
```
## Contributing
All pull requests and issues welcome!
If you're not sure how, check out Kent C. Dodds'
[great video tutorials on egghead.io](https://egghead.io/lessons/javascript-identifying-how-to-contribute-to-an-open-source-project-on-github)!
## Author & License
`sw-register` was created by [Sam Gluck](https://twitter.com/sdgluck) and is released under the MIT license.