-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathservice-worker.js
More file actions
82 lines (74 loc) · 2.48 KB
/
service-worker.js
File metadata and controls
82 lines (74 loc) · 2.48 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
77
78
79
80
81
82
'use strict';
// The cache name should be updated any time the cached files change
const CACHE_NAME = 'static-cache-v18';
const THIRD_PARTY_CACHE = 'third-party-cache-v6';
const FILES_TO_CACHE = [
'/',
'/index.html',
'/js/game.js',
'/js/maze.js',
'/js/controls.js',
'/js/storage.js',
'/js/detectmobilebrowser.js',
'/js/THREE.MeshLine.js',
'/models/wall.glb',
'/models/arrow.glb',
'/textures/dot.png',
];
const RESOURCES_TO_CACHE = [
'https://unpkg.com/three@0.181.0/build/three.module.js',
'https://unpkg.com/three@0.181.0/examples/jsm/loaders/GLTFLoader.js',
'https://unpkg.com/es-module-shims@1.3.6/dist/es-module-shims.js',
]
self.addEventListener('install', (evt) => {
console.log('[ServiceWorker] Install');
evt.waitUntil(
caches.open(CACHE_NAME).then((cache) => {
console.log('[ServiceWorker] Pre-caching pages for offline');
return cache.addAll(FILES_TO_CACHE);
})
);
evt.waitUntil(
caches.open(THIRD_PARTY_CACHE).then((cache) => {
console.log('[ServiceWorker] Pre-caching external pages for offline');
RESOURCES_TO_CACHE.forEach((url) => {
fetch(url)
.then((response) => {
// If the response was good, clone it and store it in the cache.
if (response.status === 200) {
cache.put(url, response.clone());
} else {
return Promise.reject();
}
});
});
return Promise.resolve();
})
);
self.skipWaiting();
});
self.addEventListener('activate', (evt) => {
console.log('[ServiceWorker] Activate');
evt.waitUntil(
caches.keys().then((keyList) => {
return Promise.all(keyList.map((key) => {
if (key !== CACHE_NAME && key !== THIRD_PARTY_CACHE) {
console.log('[ServiceWorker] Removing old cache', key);
return caches.delete(key);
}
}));
})
);
self.clients.claim();
});
self.addEventListener('fetch', (evt) => {
console.log('[ServiceWorker] Fetch', evt.request.url);
evt.respondWith(
caches.open(CACHE_NAME).then((cache) => {
return cache.match(evt.request)
.then((response) => {
return response || fetch(evt.request);
});
})
);
});