-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsw.js
81 lines (75 loc) · 2.57 KB
/
sw.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
const CACHE_NAME = 'opguides-cache-v0.2';
const INSTALL_CACHE = ['/404.html', '/offline/'];
/**
* Create a new cache and add all install dependencies
*/
self.addEventListener('install', (event) => {
event.waitUntil(
(async function () {
let cache = await caches.open(CACHE_NAME);
await cache.addAll(INSTALL_CACHE);
})()
);
});
/**
* Delete all old caches when outdated service worker exits
* (This only runs when this file is updated)
*/
self.addEventListener('activate', (event) => {
event.waitUntil(
(async function () {
let cacheNames = await caches.keys();
for (let cacheName of cacheNames) {
if (cacheName != CACHE_NAME) {
await caches.delete(cacheName);
}
}
})()
);
});
/**
* Implementation of the Stale-while-revalidate pattern
* (https://web.dev/offline-cookbook/#stale-while-revalidate)
*
* Essentially, take cache first for faster load times, but update the cache
* in the background so that page edits will load on the second visit.
*/
self.addEventListener('fetch', (event) => {
event.respondWith(
(async function () {
let cache = await caches.open(CACHE_NAME);
let url = new URL(event.request.url);
// Get cached response first
let cacheResponse = await caches.match(event.request);
// Send network request next and allow it to run in the background
let networkResponsePromise = fetch(event.request).then(async function (response) {
// Don't cache a 404 page for a random URL and
// only return a 404 page if this resource is ours
if (
(!response || response.status == 404)
&& url.origin == location.origin
) {
return await caches.match('/404.html');
}
if (!/^\/api\//.test(url.pathname)) {
cache.put(event.request, response.clone());
}
return response;
});
if (cacheResponse) {
// We return the cached response if it's not the first time visiting this page
return cacheResponse;
} else {
// only await on the network promise if it's needed
try {
return await networkResponsePromise;
} catch (ex) {
// only return an offline page if this resource is ours
if (url.origin == location.origin) {
return await caches.match('/offline/');
}
}
}
})()
);
});