/
sw-register.js
35 lines (32 loc) · 893 Bytes
/
sw-register.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
//This is the "Offline copy of pages" service worker
//Add this below content to your HTML page, or add the js file to your page at the very top to register service worker
(function(root, document) {
"use strict";
if (
self.location.protocol !== "https:" &&
self.location.protocol !== "http:"
) {
return;
}
var swJs =
document.currentScript && document.currentScript.dataset.serviceWorker;
if (swJs) {
if (navigator.serviceWorker.controller) {
console.log(
"[sw-register.js] Active service worker found, no need to register."
);
} else {
//Register the ServiceWorker
navigator.serviceWorker
.register(swJs, {
scope: "./"
})
.then(function(reg) {
console.log(
"[sw-register.js] Service worker has been registered for scope: " +
reg.scope
);
});
}
}
})("undefined" !== typeof window ? window : this, document);