-
Notifications
You must be signed in to change notification settings - Fork 0
/
serviceworker.js
62 lines (36 loc) · 1.01 KB
/
serviceworker.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
const cacheName = 'mapmaker';
const assets = [
'./',
'./manifest.json',
'./img/tiles/mountain.png',
'./img/bg/mapbase.png',
'./img/bg/shield.png',
'./img/tiles/cliff-tiles.png',
'./img/tiles/ruin.png',
'./img/LOGO.png',
'./css/main.css',
'./css/normalize.css',
'./js/mapmaker/mapmaker.mjs',
'./js/mapmaker/SeedGenerator.mjs',
];
self.addEventListener( 'install', async function () {
const cache = await caches.open( cacheName );
assets.forEach( function ( asset ) {
cache.add( asset ).catch( function () {
console.warn( '[SW] Cound\'t cache:', asset );
} );
} );
} );
self.addEventListener( 'fetch', async function ( event ) {
const request = event.request;
event.respondWith( networkFirst( request ) );
} );
async function networkFirst( request ) {
return fetch( request ).catch( async function () {
const cachedResponse = await caches.match( request );
if ( cachedResponse === undefined ) {
console.warn( '[SW] Not cached:', request.url );
}
return cachedResponse;
} );
}