-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsw.js
80 lines (77 loc) · 2.21 KB
/
sw.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"use strict";
const PREF ='BQ', CACHE = PREF+'01'
// automate file things with WebPage that show everything underit with the needed style.
const FILES = [
'/Rehber/',
// '/Rehber/finder.html',
// '/Rehber/reader.html',
// '/Rehber/code/buckwalter.js',
// '/Rehber/code/common.js',
// '/Rehber/code/finder.js',
// '/Rehber/code/quran-data.js',
// '/Rehber/code/quran-sura.js',
// '/Rehber/code/script.js',
// '/Rehber/data/ar.jalalayn.txt',
// '/Rehber/data/ar.muyassar.txt',
// '/Rehber/data/en.ahmedali.txt',
// '/Rehber/data/en.yusufali.txt',
// '/Rehber/data/quran-simple-clean.txt',
// '/Rehber/data/quran-uthmani.txt',
// '/Rehber/data/tr.diyanet.txt',
// '/Rehber/data/tr.yazir.txt',
'/Rehber/style/me_quran.ttf',
'https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css',
'https://code.jquery.com/jquery-3.3.1.slim.min.js',
'https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js',
'https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js',
// '/Rehber/style/keyboard.css',
// '/Rehber/code/keyboard.js',
// '/Rehber/code/language.js',
'/Rehber/images/search.png'
]
function installCB(e) { //CB means call-back
console.log("installing "+CACHE);
e.waitUntil(
caches.open(CACHE)
.then(cache => cache.addAll(FILES))
.catch(console.log)
)
}
addEventListener('install', installCB)
function save(req, resp) {
if (!req.url.includes("Rehber"))
return resp;
return caches.open(CACHE)
.then(cache => { // save request
cache.put(req, resp.clone());
return resp;
})
.catch(console.err)
}
function report(req) {
console.log(CACHE+' matches '+req.url)
return req
}
function fetchCB(e) { //fetch first
let req = e.request
e.respondWith(
fetch(req).then(r2 => save(req, r2))
.catch(() => caches.match(req).then(report))
)
}
addEventListener('fetch', fetchCB)
function removeOld(L) {
return Promise.all(L.map(key => {
if (!key.startsWith(PREF) || key == CACHE)
return null;
console.log(key+" is deleted")
return caches.delete(key)
}))
}
function activateCB(e) {
console.log(CACHE+" is activated");
e.waitUntil(
caches.keys().then(removeOld)
)
}
addEventListener('activate', activateCB);