-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsw.js
66 lines (58 loc) · 1.78 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
// 1. Put here the name of your game:
const cachePrefix = "lumber-chop";
// 2. Increment the version every time you publish your game!
const cacheName = cachePrefix + "-v72";
const cacheList = [
"https://yandex.ru/games/sdk/v2",
// 3. Add ALL your game files to the list:
"index.html",
"dmloader.js",
"Squarefall.wasm",
"Squarefall_wasm.js",
"Squarefall_asmjs.js",
"archive/archive_files.json",
"archive/game.arcd0",
"archive/game.arci0",
"archive/game.dmanifest0",
"archive/game.projectc0",
"archive/game.public.der0",
];
// Installation of the service worker
self.addEventListener("install", function (event) {
event.waitUntil(
caches.open(cacheName).then((cache) => {
return cache.addAll(cacheList);
})
);
});
// Deletion of old cache
self.addEventListener("activate", function (event) {
console.assert(typeof cacheName === "string");
console.assert(typeof cachePrefix === "string");
event.waitUntil(
caches.keys().then((keyList) => {
return Promise.all(
keyList.map((key) => {
if (key.indexOf(cachePrefix) === 0 && key !== cacheName) {
return caches.delete(key);
}
})
);
})
);
});
// Handling of data stored in the device cache with exceptions
self.addEventListener("fetch", function (event) {
if (
event.request.method !== "GET" ||
event.request.url.indexOf("http://") === 0 ||
event.request.url.indexOf("an.yandex.ru") !== -1
) {
return;
}
event.respondWith(
caches.match(event.request, { ignoreSearch: true }).then(function (response) {
return response || fetch(event.request);
})
);
});