-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
51 lines (44 loc) · 1.43 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
// Path to all static assests that will be stored in chache
const staticAssets = [
'./',
'./style.css',
'./app.js',
'./utils.js',
'./themes/in-sangbok-pwa.min.css',
'./themes/jquery-1.11.1.min.js',
'./themes/jquery.mobile-1.4.5.min.js',
'./themes/jquery.mobile.icons.min.css',
'./themes/jquery.mobile.structure-1.4.5.min.css'
];
// When installing the application it will cache the static assets.
self.addEventListener('install', async event => {
const cache = await caches.open('sangbok-static');
cache.addAll(staticAssets);
console.log("install");
});
// When fetching it will either load from cache or newtork. If online it will load from online etc.
self.addEventListener('fetch', event => {
const req = event.request;
const url = new URL(req.url);
if (url.origin === location.origin) {
event.respondWith(cacheFirst(req));
} else {
event.respondWith(networkFirst(req));
}
});
// When loading from cache it will check if it can load from network.
async function cacheFirst(req) {
const cachedResponse = await caches.match(req);
return cachedResponse || fetch(req);
}
// It will store xml in cache.
async function networkFirst(req) {
const cache = await caches.open('sangbok-dynamic');
try {
const res = await fetch(req);
cache.put(req, res.clone());
return res;
} catch (error) {
return await chaches.match(req);
}
}