-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsw.js
148 lines (148 loc) · 6.12 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
let sw_version = "02";
let cache_version_number = "01";
(function() {
var cache_version = 'bitnote_' + cache_version_number;
var pre_cache_files = ['/', 'sw.js', 'pages/js/zxcvbn.js', 'pages/js/ww.js', 'manifest.json', 'pages/js/new_wl.js', 'pages/fonts/bitnote_box_font.woff2', 'pages/fonts/inter_variable.ttf', ];
var pre_load_files = ['/', 'sw.js', 'pages/js/ww.js', 'manifest.json', 'pages/js/new_wl.js', 'pages/fonts/bitnote_box_font.woff2', 'pages/fonts/inter_variable.ttf', ];
var non_static_files = ['/', 'sw.js', 'pages/js/ww.js', 'manifest.json', ];
function checkVersion(cb) {
fetch("/pages/live_version.txt?_=" + Date.now()).then(function(response) {
const reader = response.body.getReader();
reader.read().then(function processText({done, value}) {
var version = new TextDecoder().decode(value).split('\n')[0];
cb(version);
});
});
}
self.addEventListener('install', function(event) {
event.waitUntil(caches.open(cache_version).then(function(cache) {
cache.keys().then(function(keys) {
if (keys.length === 0)
return cache.addAll(pre_cache_files);
});
}));
console.log("skipping install wait");
self.skipWaiting();
});
self.addEventListener('activate', function(event) {
console.log("waiting on activate claim");
event.waitUntil(self.clients.claim());
});
function updateVersion(full_clear=false, cb) {
(function clearCache() {
caches.keys().then(function(keys) {
if (full_clear) {
Promise.all(keys.map(function(cacheName) {
return caches.delete(cacheName);
}));
} else {
return Promise.all(keys.map(function(key) {
return caches.open(key).then(function(cache) {
return Promise.all(non_static_files.map(function(filename) {
return cache.delete(filename);
}));
});
}));
}
}).then(updateCache);
}
)();
function updateCache() {
caches.open(cache_version).then(function(cache) {
if (full_clear)
hardRefresh(pre_cache_files, cache);
else
hardRefresh(non_static_files, cache);
});
function hardRefresh(array_list, cache) {
var date = Date.now();
var file_list = array_list.map(function(filename) {
return filename + "?_" + date
});
var first = file_list.shift();
fetch(first).then(function(response) {
cacheResponse(response);
var promises = file_list.map(function(file) {
return fetch(file).then(cacheResponse);
});
Promise.all(promises).then(cb);
});
function cacheResponse(response) {
if (response.status === 200)
return cache.put(response.url.split('?')[0], response);
}
}
}
}
self.addEventListener('fetch', standardFetch);
function standardFetch(e) {
if (e.request.method === 'GET') {
e.respondWith(caches.open(cache_version).then(function(cache) {
var match_url = stripUsername(e.request);
return cache.match(match_url, {
ignoreSearch: true
}).then(function(response) {
return response || fetch(e.request).then(function(response) {
if (response.status === 200) {
if (e.request.url.includes(self.location.hostname)) {
var temp_url = e.request.url.replace(self.location.hostname, "");
if (pre_cache_files.includes(temp_url))
cache.put(e.request, response.clone());
}
}
return response;
});
});
}));
}
}
function stripUsername(request) {
var url = new URL(request.url);
if (url.pathname != "/" && url.origin === self.location.origin) {
var username = url.pathname.match(/\/.*?\//);
if (checkForProperty(username)) {
username = username[0].replaceAll("/", "");
if (username != "pages")
return url.origin;
}
}
return request;
}
self.addEventListener('message', messageReceiver);
function messageReceiver(msg) {
if (msg.ports) {
switch (msg.data.type) {
case "init":
checkVersion(function(ret) {
console.log(ret);
msg.ports[0].postMessage({
type: "version_check",
sw_version: sw_version,
live_version: ret
});
});
break;
case "update_version":
updateVersion(false, function(ret) {
msg.ports[0].postMessage({
type: "update_result",
response: ret
});
});
break;
case "clear_cache":
updateVersion(true, function(ret) {
msg.ports[0].postMessage({
type: "clear_cache",
response: ret
});
});
break;
}
}
}
function checkForProperty(prop) {
return (prop === "" || prop === null || prop === undefined) ? false : true;
}
}
)();