https://github.com/yyx990803/register-service-worker
A script to simplify service worker registration with hooks for common events.
https://github.com/yyx990803/register-service-worker
Last synced: 10 months ago
JSON representation
A script to simplify service worker registration with hooks for common events.
- Host: GitHub
- URL: https://github.com/yyx990803/register-service-worker
- Owner: yyx990803
- License: mit
- Created: 2018-01-10T21:07:58.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2024-04-17T09:35:04.000Z (almost 2 years ago)
- Last Synced: 2025-05-07T22:39:18.736Z (11 months ago)
- Language: JavaScript
- Size: 97.7 KB
- Stars: 656
- Watchers: 15
- Forks: 58
- Open Issues: 22
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- awesome - register-service-worker - A script to simplify service worker registration with hooks for common events. (PWA)
README
# register-service-worker
A script to simplify service worker registration with hooks for common events.
## Usage
**Note:** this script uses ES modules export and is expected to be used with a client side bundler that can handle ES modules syntax.
``` js
import { register } from 'register-service-worker'
register('/service-worker.js', {
registrationOptions: { scope: './' },
ready (registration) {
console.log('Service worker is active.')
},
registered (registration) {
console.log('Service worker has been registered.')
},
cached (registration) {
console.log('Content has been cached for offline use.')
},
updatefound (registration) {
console.log('New content is downloading.')
},
updated (registration) {
console.log('New content is available; please refresh.')
},
offline () {
console.log('No internet connection found. App is running in offline mode.')
},
error (error) {
console.error('Error during service worker registration:', error)
}
})
```
The `ready`, `registered`, `cached`, `updatefound` and `updated` events passes a [ServiceWorkerRegistration](https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration) instance in their arguments.
The `registrationOptions` object will be passed as the second argument to [`ServiceWorkerContainer.register()`](https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/register#Parameters)