@@ -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) {