-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
120 lines (102 loc) · 2.32 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
"use strict";
const APP_CACHE = "berzanjs-app-cache-v1.2.6";
const CONTENT_CACHE = "berzanjs-schedule-cache-v1";
const URLS_TO_CACHE = [
"/",
"/css/style.css",
"/js/index.js",
"/js/settings.js",
"/js/utils.js",
"/views/neoschedule.html",
"/views/etc.html",
"/views/settings.html",
"/views/about.html",
"/manifest.json",
"/img/logo/logo-512.png",
"https://cdnjs.cloudflare.com/ajax/libs/slideout/1.0.1/slideout.js"
];
let refreshing = false;
self.addEventListener("install", function(evnt) {
skipWaiting();
evnt.waitUntil(
caches.open(APP_CACHE)
.then(function(cache) {
console.log("Opened cache");
return cache.addAll(URLS_TO_CACHE);
})
);
});
self.addEventListener("activate", function(evnt) {
const CACHE_WHITELIST = [
APP_CACHE,
CONTENT_CACHE
];
evnt.waitUntil(
caches.keys().then(cacheNames => {
return Promise.all(
cacheNames.map(cacheName => {
if (CACHE_WHITELIST.indexOf(cacheName) === -1) {
return caches.delete(cacheName);
}
})
);
})
);
});
self.addEventListener("fetch", function(evnt){
evnt.respondWith(
caches.match(evnt.request)
.then(function(response) {
if (response) {
// TODO: refresh caches
/*
if (response.type === "cors" && evnt.clientId) {
refreshCacheEntry(evnt.request, evnt.clientId);
}
*/
return response;
}
return fetch(evnt.request).then(
function(response) {
if (
!response ||
response.status !== 200 ||
response.type !== "basic"
) {
return response;
}
const RESPONSE_TO_CACHE = response.clone();
let cacheType;
if (response.type === "cors") {
cacheType = CONTENT_CACHE;
} else {
cacheType = APP_CACHE;
}
caches.open(cacheType)
.then(function(cache) {
cache.put(evnt.request, RESPONSE_TO_CACHE);
});
return response;
}
);
})
);
});
self.addEventListener("controllerchange", () => {
if (refreshing) {
return; // Prevent infinite refresh loops
}
refreshing = true;
window.location.reload();
});
async function refreshCacheEntry(request, clientID) {
fetch(request)
.then(async response => {
const client = await self.clients.get(clientID);
client.postMessage({
URL: request.url,
response: response
});
//caches.match(request)
})
}