-
Notifications
You must be signed in to change notification settings - Fork 2
/
service-worker.js
110 lines (91 loc) · 2.87 KB
/
service-worker.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
'use strict';
// The name of your game, no spaces or special characters.
const name = 'Monogatari';
// The version of the cache, changing this will force everything to be cached
// again.
const version = '0.3.0';
const files = [
'/',
// General Files
'manifest.json',
// Engine Files
'engine/core/monogatari.css',
'engine/core/monogatari.js',
// HTML Files
'index.html',
// Style Sheets
'style/main.css',
// JavaScript Files
'js/options.js',
'js/storage.js',
'js/script.js',
'js/main.js',
// App Images
'favicon.ico',
'assets/icons/icon_48x48.png',
'assets/icons/icon_60x60.png',
'assets/icons/icon_70x70.png',
'assets/icons/icon_76x76.png',
'assets/icons/icon_96x96.png',
'assets/icons/icon_120x120.png',
'assets/icons/icon_128x128.png',
'assets/icons/icon_150x150.png',
'assets/icons/icon_152x152.png',
'assets/icons/icon_167x167.png',
'assets/icons/icon_180x180.png',
'assets/icons/icon_192x192.png',
'assets/icons/icon_310x150.png',
'assets/icons/icon_310x310.png',
'assets/icons/icon_512x512.png'
];
self.addEventListener ('install', (event) => {
self.skipWaiting ();
event.waitUntil (
caches.open (`${name}-v${version}`).then ((cache) => {
return cache.addAll (files);
})
);
});
self.addEventListener ('activate', (event) => {
event.waitUntil (
caches.keys ().then ((keyList) => {
return Promise.all (keyList.map ((key) => {
if (key !== `${name}-v${version}`) {
return caches.delete (key);
}
}));
})
);
return self.clients.claim ();
});
self.addEventListener ('fetch', (event) => {
if (event.request.method !== 'GET') {
return;
}
event.respondWith (
caches.match (event.request).then ((cached) => {
function fetchedFromNetwork (response) {
const cacheCopy = response.clone ();
caches.open (`${name}-v${version}`).then (function add (cache) {
cache.put (event.request, cacheCopy);
});
return response;
}
function unableToResolve () {
return new Response (`
<!DOCTYPE html><html lang=en><title>Bad Request</title><meta charset=UTF-8><meta content="width=device-width,initial-scale=1"name=viewport><style>body,html{width:100%;height:100%}body{text-align:center;color:#545454;margin:0;display:flex;justify-content:center;align-items:center;font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Oxygen,Ubuntu,Cantarell,"Open Sans","Fira Sans","Droid Sans","Helvetica Neue",sans-serif}h1,h2{font-weight:lighter}h1{font-size:4em}h2{font-size:2em}</style><div><h1>Service Unavailable</h1><h2>Sorry, the server is currently unavailable or under maintenance, try again later.</h2></div>
`, {
status: 503,
statusText: 'Service Unavailable',
headers: new Headers ({
'Content-Type': 'text/html'
})
});
}
const networked = fetch (event.request)
.then (fetchedFromNetwork, unableToResolve)
.catch (unableToResolve);
return cached || networked;
})
);
});