-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
49 lines (38 loc) · 1.43 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// See docs for options info
// https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/register
function swRegistration(path, options) {
if (typeof path !== 'string') {
throw new Error('Function swRegistration requires "path" to be a string.');
}
options = options || {};
return navigator.serviceWorker.register(path, options)
.then(swRegistrationSuccess.bind(this, path))
.catch(swRegistrationError);
};
function swRegistrationSuccess(path, registration) {
if (registration.installing) {
console.log('Service worker at "' + path + '" is installing.');
} else if (registration.waiting) {
console.log('Service worker at "' + path + '" is installed.');
} else if (registration.active) {
console.log('Service worker at "' + path + '" is active.');
}
// Service worker is now registered and now runs inside of a Worker context
// and therefore has no DOM access.
};
function swRegistrationError (error) {
console.log('SW Registration failed with error: ', error);
};
function swRegistrationComplete () {
// Do other things here :)
};
function onLoad () {
// Test if the browser can support Service Workers.
// The origin must also be served over HTTPS except localhost
if ('serviceWorker' in navigator) {
swRegistration('/sw.js').then(swRegistrationComplete)
} else {
console.log('Your browser does not support service workers.');
}
};
window.addEventListener('load', onLoad);