forked from pH7Software/pH7-Social-Dating-CMS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service-worker.js
70 lines (59 loc) · 2.13 KB
/
service-worker.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
/*
@see /templates/system/modules/pwa/themes/base/js/sw-register.js
TODO: Implement the Service Worker to cache some static data, html pages, images, etc.
More info: https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API/Using_Service_Workers
🚀 Feel free to fork the project: https://github.com/pH7Software/pH7-Social-Dating-CMS
Commit your changes, then open a Pull Request for submitting your awesome changes 🎉
*/
const STATIC_CACHE_NAME = 'sta_homepage';
const DYNAMIC_CACHE_NAME = 'dyn_homepage';
const CACHED_FILES = [
'/',
// TODO: Add more URLs to be cached here
];
self.addEventListener('install', async event => {
const cache = await caches.open(STATIC_CACHE_NAME);
await cache.addAll(CACHED_FILES);
});
self.addEventListener('fetch', event => {
const {request} = event;
const url = new URL(request.url);
if (url.origin === location.origin) {
event.respondWith(cacheData(request));
} else {
event.respondWith(networkFirst(request));
}
});
async function cacheData(request) {
const cachedResponse = await caches.match(request);
return cachedResponse || fetch(request);
}
async function networkFirst(request) {
const cache = await caches.open(DYNAMIC_CACHE_NAME);
try {
const response = await fetch(request);
await cache.put(request, response.clone());
return response;
} catch (error) {
return await cache.match(request);
}
}
// Add to Home Screen
self.addEventListener('fetch', event => {
// Workaround for https://bugs.chromium.org/p/chromium/issues/detail?id=823392
if (event.request.cache === 'only-if-cached' && event.request.mode !== 'same-origin') {
return;
}
event.respondWith(
// Try to find response in cache...
caches.match(event.request)
.then((resp) => {
// If response is found in cache, then return it, otherwise fetch it
return resp || fetch(event.request);
})
.catch((err) => {
// Something went wrong
console.log("Service worker Fetch: ", err);
})
);
});