Files

Return to Package Diff Home.
Brought to you by Intrinsic.

Package Diff: register-service-worker @ 1.5.2 .. 1.6.0

CHANGELOG.md

@@ -1,31 +1,36 @@
-<a name="1.5.2"></a>
-## [1.5.2](https://github.com/yyx990803/register-service-worker/compare/v1.5.1...v1.5.2) (2018-08-16)
+<a name="1.6.0"></a>
+# [1.6.0](https://github.com/yyx990803/register-service-worker/compare/v1.5.2...v1.6.0) (2019-02-08)
### Bug Fixes
-* fix missing registration arguments ([#11](https://github.com/yyx990803/register-service-worker/issues/11)) ([d11b254](https://github.com/yyx990803/register-service-worker/commit/d11b254))
+* **types:** make hooks parameter optional ([#18](https://github.com/yyx990803/register-service-worker/issues/18)) ([283c570](https://github.com/yyx990803/register-service-worker/commit/283c570))
+* misleading error when wrong MIME type returned ([#21](https://github.com/yyx990803/register-service-worker/issues/21)) ([#22](https://github.com/yyx990803/register-service-worker/issues/22)) ([d3287d3](https://github.com/yyx990803/register-service-worker/commit/d3287d3))
+### Features
-<a name="1.5.1"></a>
-## [1.5.1](https://github.com/yyx990803/register-service-worker/compare/v1.5.0...v1.5.1) (2018-08-13)
+* registration options ([#17](https://github.com/yyx990803/register-service-worker/issues/17)) ([c6f0386](https://github.com/yyx990803/register-service-worker/commit/c6f0386))
+
+
+
+<a name="1.5.2"></a>
+## [1.5.2](https://github.com/yyx990803/register-service-worker/compare/v1.5.1...v1.5.2) (2018-08-16)
### Bug Fixes
-* **types:** add type for updatefound event ([2be9827](https://github.com/yyx990803/register-service-worker/commit/2be9827))
+* fix missing registration arguments ([#11](https://github.com/yyx990803/register-service-worker/issues/11)) ([d11b254](https://github.com/yyx990803/register-service-worker/commit/d11b254))
-<a name="1.5.0"></a>
-# [1.5.0](https://github.com/yyx990803/register-service-worker/compare/v1.4.1...v1.5.0) (2018-08-13)
+<a name="1.5.1"></a>
+## [1.5.1](https://github.com/yyx990803/register-service-worker/compare/v1.5.0...v1.5.1) (2018-08-13)
-### Features
+### Bug Fixes
-* add "updatefound" event ([#7](https://github.com/yyx990803/register-service-worker/issues/7)) ([bee2641](https://github.com/yyx990803/register-service-worker/commit/bee2641))
-* emit updated event when registration.waiting was found ([#9](https://github.com/yyx990803/register-service-worker/issues/9)) ([937040f](https://github.com/yyx990803/register-service-worker/commit/937040f))
+* **types:** add type for updatefound event ([2be9827](https://github.com/yyx990803/register-service-worker/commit/2be9827))

index.d.ts

@@ -1,4 +1,5 @@
export type Hooks = {
+ registrationOptions: RegistrationOptions;
ready?: (registration: any) => void;
registered?: (registration: any) => void;
cached?: (registration: any) => void;
@@ -8,6 +9,6 @@
error?: (error: Error) => void;
};
-export function register(swUrl: string, hooks: Hooks): void;
+export function register(swUrl: string, hooks?: Hooks): void;
export function unregister(): void;

index.js

@@ -16,6 +16,11 @@
); }
export function register (swUrl, hooks) {
+ if ( hooks === void 0 ) hooks = {};
+
+ var registrationOptions = hooks.registrationOptions; if ( registrationOptions === void 0 ) registrationOptions = { scope: '/' };
+ delete hooks.registrationOptions
+
var emit = function (hook) {
var args = [], len = arguments.length - 1;
while ( len-- > 0 ) args[ len ] = arguments[ len + 1 ];
@@ -29,21 +34,21 @@
window.addEventListener('load', function () {
if (isLocalhost()) {
// This is running on localhost. Lets check if a service worker still exists or not.
- checkValidServiceWorker(swUrl, emit)
+ checkValidServiceWorker(swUrl, emit, registrationOptions)
navigator.serviceWorker.ready.then(function (registration) {
emit('ready', registration)
})
} else {
// Is not local host. Just register service worker
- registerValidSW(swUrl, emit)
+ registerValidSW(swUrl, emit, registrationOptions)
}
})
}
}
-function registerValidSW (swUrl, emit) {
+function registerValidSW (swUrl, emit, registrationOptions) {
navigator.serviceWorker
- .register(swUrl)
+ .register(swUrl, registrationOptions)
.then(function (registration) {
emit('registered', registration)
if (registration.waiting) {
@@ -76,21 +81,23 @@
})
}
-function checkValidServiceWorker (swUrl, emit) {
+function checkValidServiceWorker (swUrl, emit, registrationOptions) {
// Check if the service worker can be found.
fetch(swUrl)
.then(function (response) {
// Ensure service worker exists, and that we really are getting a JS file.
- if (
- response.status === 404 ||
- response.headers.get('content-type').indexOf('javascript') === -1
- ) {
+ if (response.status === 404) {
// No service worker found.
emit('error', new Error(("Service worker not found at " + swUrl)))
unregister()
+ } else if (response.headers.get('content-type').indexOf('javascript') === -1) {
+ emit('error', new Error(
+ "Expected " + swUrl + " to have javascript content-type, " +
+ "but received " + (response.headers.get('content-type'))))
+ unregister()
} else {
// Service worker found. Proceed as normal.
- registerValidSW(swUrl, emit)
+ registerValidSW(swUrl, emit, registrationOptions)
}
})
.catch(function (error) {

package.json

@@ -1,7 +1,7 @@
{
"name": "register-service-worker",
"sideEffects": false,
- "version": "1.5.2",
+ "version": "1.6.0",
"description": "Script for registering service worker, with hooks",
"main": "index.js",
"scripts": {

README.md

@@ -10,7 +10,8 @@
import { register } from 'register-service-worker'
register('/service-worker.js', {
- ready () {
+ registrationOptions: { scope: './' },
+ ready (registration) {
console.log('Service worker is active.')
},
registered (registration) {
@@ -34,4 +35,4 @@
})
```
-The `cached`, `updatefound` and `updated` events passes a [ServiceWorkerRegistration](https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration) instance in their arguments.
+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.

src/index.js

@@ -15,7 +15,10 @@
)
)
-export function register (swUrl, hooks) {
+export function register (swUrl, hooks = {}) {
+ const { registrationOptions = { scope: '/' }} = hooks
+ delete hooks.registrationOptions
+
const emit = (hook, ...args) => {
if (hooks && hooks[hook]) {
hooks[hook](...args)
@@ -26,21 +29,21 @@
window.addEventListener('load', () => {
if (isLocalhost()) {
// This is running on localhost. Lets check if a service worker still exists or not.
- checkValidServiceWorker(swUrl, emit)
+ checkValidServiceWorker(swUrl, emit, registrationOptions)
navigator.serviceWorker.ready.then(registration => {
emit('ready', registration)
})
} else {
// Is not local host. Just register service worker
- registerValidSW(swUrl, emit)
+ registerValidSW(swUrl, emit, registrationOptions)
}
})
}
}
-function registerValidSW (swUrl, emit) {
+function registerValidSW (swUrl, emit, registrationOptions) {
navigator.serviceWorker
- .register(swUrl)
+ .register(swUrl, registrationOptions)
.then(registration => {
emit('registered', registration)
if (registration.waiting) {
@@ -73,21 +76,23 @@
})
}
-function checkValidServiceWorker (swUrl, emit) {
+function checkValidServiceWorker (swUrl, emit, registrationOptions) {
// Check if the service worker can be found.
fetch(swUrl)
.then(response => {
// Ensure service worker exists, and that we really are getting a JS file.
- if (
- response.status === 404 ||
- response.headers.get('content-type').indexOf('javascript') === -1
- ) {
+ if (response.status === 404) {
// No service worker found.
emit('error', new Error(`Service worker not found at ${swUrl}`))
unregister()
+ } else if (response.headers.get('content-type').indexOf('javascript') === -1) {
+ emit('error', new Error(
+ `Expected ${swUrl} to have javascript content-type, ` +
+ `but received ${response.headers.get('content-type')}`))
+ unregister()
} else {
// Service worker found. Proceed as normal.
- registerValidSW(swUrl, emit)
+ registerValidSW(swUrl, emit, registrationOptions)
}
})
.catch(error => {