-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserviceworker.js
More file actions
112 lines (96 loc) · 3.51 KB
/
serviceworker.js
File metadata and controls
112 lines (96 loc) · 3.51 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
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
111
112
(() => {
// Update 'version' if you need to refresh the cache
const cacheVersion = 'v1.0.3';
const baseUrl = 'https://wireshell.pw';
const alwaysCache = [
'/',
'/offline/',
`/assets/js/main.min.js`,
`/assets/css/main.min.css`,
'/assets/img/sprites.svg'
];
const neverCache = [
'/serviceworker.js'
];
// Store core files in a cache (including a page to display when offline)
const updateStaticCache = () => caches.open(cacheVersion)
.then((cache) => cache.addAll(alwaysCache));
self.addEventListener('install', (e) => {
e.waitUntil(updateStaticCache());
});
self.addEventListener('activate', (e) => {
// Remove caches whose name is no longer valid
e.waitUntil(caches.keys()
.then((keys) => Promise.all(
keys
.filter((key) => key.indexOf(cacheVersion) !== 0)
.map((key) => caches.delete(key))
)
)
);
});
self.addEventListener('fetch', (e) => {
let request = e.request;
// Always fetch non-GET requests from the network
if (request.method !== 'GET') {
e.respondWith(fetch(request).catch(() => caches.match('/offline/')));
return;
}
// if we have a reqest, that matches in neverCache, always return from network
if (neverCache.some((item) => (new RegExp(`\\b${item}\\b`)).test(request.url.replace(baseUrl, '')))) {
e.respondWith(fetch(request).catch(() => caches.match('/offline/')));
}
// For HTML requests, try the network first, fall back to the cache, finally the offline page
if (request.headers.get('Accept').indexOf('text/html') !== -1) {
// Fix for Chrome bug: https://code.google.com/p/chromium/issues/detail?id=573937
if (request.mode !== 'navigate') {
request = new Request(request.url, {
method: 'GET',
headers: request.headers,
mode: request.mode === 'navigate' ? 'cors' : request.mode,
credentials: request.credentials,
redirect: request.redirect
});
}
e.respondWith(fetch(request)
.then(response => {
const response2 = response.clone();
// Stash a copy of this page in the cache
caches.open(cacheVersion)
.then(cache => {
cache.put(request, response2);
});
return response;
})
.catch(() => caches.match(request)
.then((response) => response || caches.match('/offline/'))
)
);
return;
}
// For non-HTML requests, look in the cache first, fall back to the network
e.respondWith(caches.match(request)
.then((response) => response || fetch(request)
.catch(() => {
// If the request is for an image, show an offline placeholder
if (request.headers.get('Accept').indexOf('image') !== -1) {
return new Response(`
<svg width="400" height="300" role="img" aria-labelledby="offline-title"
viewBox="0 0 400 300" xmlns="http://www.w3.org/2000/svg">
<title id="offline-title">Offline</title>
<g fill="none" fill-rule="evenodd">
<path fill="#D8D8D8" d="M0 0h400v300H0z"/>
<text fill="#9B9B9B" font-family="Arial, sans-serif" font-size="72" font-weight="bold">
<tspan x="93" y="172">offline</tspan>
</text>
</g>
</svg>`,
{ headers: { 'Content-Type': 'image/svg+xml' } }
);
}
return false;
})
)
);
});
})();