Repository navigation
Expand file tree
/
Copy pathsw.js
More file actions
76 lines (73 loc) · 2.89 KB
/
Copy pathsw.js
File metadata and controls
76 lines (73 loc) · 2.89 KB
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
// Service worker: caches the whole game so it works offline once loaded.
// Bump VERSION whenever files change so players get the update.
const VERSION = 'minicrafter-v33';
const CORE = [
'./', './index.html', './style.css', './privacy.html', './terms.html', './legal.css', './features.html', './features.css', './manifest.webmanifest', './site-config.json',
'./icons/icon-192.png', './icons/icon-512.png', './icons/icon-maskable-512.png', './icons/apple-touch-icon.png', './icons/favicon-32.png',
'./vendor/three.module.js', './vendor/blockly.min.js', './vendor/media/sprites.png',
'./src/a11y.js',
'./src/adventure.js',
'./src/audio.js',
'./src/blocks.js',
'./src/combat.js',
'./src/spark.js',
'./src/together.js',
'./src/account.js',
'./src/course.js',
'./src/photo.js',
'./src/insights.js',
'./src/news.js',
'./src/craftui.js',
'./src/petui.js',
'./src/critters.js',
'./src/ideas.js',
'./src/inventory.js',
'./src/items.js',
'./src/main.js',
'./src/mobs.js',
'./src/noise.js',
'./src/particles.js',
'./src/player.js',
'./src/quests.js',
'./src/robot.js',
'./src/robotblocks.js',
'./src/robotui.js',
'./src/share.js',
'./src/sky.js',
'./src/survival.js',
'./src/textures.js',
'./src/tools.js',
'./src/touch.js',
'./src/villages.js',
'./src/world.js',
'./src/worlds.js',
];
self.addEventListener('install', (e) => {
// cache: 'reload' skips the browser's own HTTP cache, so a new version never picks up old files.
e.waitUntil(caches.open(VERSION).then((c) => c.addAll(CORE.map((u) => new Request(u, { cache: 'reload' })))).then(() => self.skipWaiting()));
});
self.addEventListener('activate', (e) => {
e.waitUntil(
caches.keys()
.then((keys) => Promise.all(keys.filter((k) => k !== VERSION).map((k) => caches.delete(k))))
.then(() => self.clients.claim()),
);
});
// Stale-while-revalidate for our own files: instant (and offline) from the
// cache, refreshed in the background when online.
self.addEventListener('fetch', (e) => {
const req = e.request;
const url = new URL(req.url);
if (req.method !== 'GET' || url.origin !== self.location.origin) return;
if (url.pathname.includes('/api/') || url.pathname.endsWith('/rooms')) return; // accounts and Play Together are always live, never cached
e.respondWith(caches.open(VERSION).then(async (cache) => {
const cached = await cache.match(req, { ignoreSearch: req.mode === 'navigate' });
// no-cache: always check with the server (a cheap "not changed" answer when nothing changed).
const fresh = req.mode === 'navigate' ? new Request(req.url, { cache: 'no-cache' }) : new Request(req, { cache: 'no-cache' });
const network = fetch(fresh).then((res) => {
if (res.ok) cache.put(req, res.clone());
return res;
}).catch(() => null);
return cached || (await network) || (req.mode === 'navigate' ? cache.match('./index.html') : Response.error());
}));
});