-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
libresilient.js
41 lines (39 loc) · 1.9 KB
/
libresilient.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
/*
* minimal LibResilient service worker loading script
* include it in your HTML files to deploy LibResilient
*
* for a more complete and fancy implementation of browser-side
* see `libresilient-fancy.js`
*/
// check if the browser implements ServiceWorkers API
if ('serviceWorker' in navigator) {
if (navigator.serviceWorker.controller) {
// Service worker already registered.
console.log('A Service Worker is already registered.')
} else {
// NOTICE: Assumptions made here:
// 1. the Service Worker script is called service-worker.js
// 2. ...and is located in the same directory as this file
var scriptPath = document.currentScript.src
var scriptFolder = scriptPath.substr(0, scriptPath.lastIndexOf( '/' )+1 )
var serviceWorkerPath = scriptFolder + 'service-worker.js'
console.log('LibResilient: using Service Worker script at: ' + serviceWorkerPath)
// this code actually finally registers the Service Worker
navigator.serviceWorker.register(serviceWorkerPath, {
// NOTICE: "There is frequent confusion surrounding the meaning and use of scope.
// NOTICE: Since a service worker can't have a scope broader than its own location,
// NOTICE: only use the scope option when you need a scope that is narrower than the default."
//
// so, "./" is the broadest scope, also the default. but the ServiceWorker can be registered
// for a narrower scope, for example "./subdir/".
scope: '/'
}).then(function(reg) {
// Success.
console.log('LibResilient: Service Worker registered.')
}).catch(error => {
console.error("LibResilient: Error while registering a service worker!", error)
})
}
} else {
console.warn("unable to load LibResilient: ServiceWorker API not available in the browser")
}