-
Notifications
You must be signed in to change notification settings - Fork 0
/
serviceworker.js
92 lines (82 loc) · 3.25 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
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
const CACHE_NAME = 'cache-2023-06-22T17:51:26.246'
const OFFLINE_PAGE_URL = '/offline/'
const PRECACHE_RESOURCES = []
const IGNORED_HOSTS = ['localhost']
// On Install
self.addEventListener('install', (event) => {
event.waitUntil(
(async () => {
const cache = await caches.open(CACHE_NAME)
// add all resources we want to precache first
if (PRECACHE_RESOURCES.length) {
cache.addAll(PRECACHE_RESOURCES)
}
// then add the offline page.
// Setting {cache: 'reload'} in the new request will ensure that the
// response isn't fulfilled from the HTTP cache; i.e., it will be from
// the network.
await cache.add(new Request(OFFLINE_PAGE_URL, { cache: 'reload' }))
})()
)
// Force the waiting service worker to become the active service worker.
self.skipWaiting()
})
// On Activation
self.addEventListener('activate', (event) => {
event.waitUntil(
(async () => {
// Enable navigation preload if it's supported.
// See https://developers.google.com/web/updates/2017/02/navigation-preload
if ('navigationPreload' in self.registration) {
await self.registration.navigationPreload.enable()
}
// clear out any old caches that might still be around
const cacheNames = await caches.keys()
await Promise.all(
cacheNames.map((cacheName) => {
if (cacheName !== CACHE_NAME) {
return caches.delete(cacheName)
}
})
)
})()
)
// Tell the active service worker to take control of the page immediately.
self.clients.claim()
})
// On Request
self.addEventListener('fetch', (event) => {
const { hostname } = new URL(event.request.url)
if (IGNORED_HOSTS.indexOf(hostname) >= 0) {
return
}
// For navigation requests (GET to a new location)
if (event.request.mode === 'navigate') {
event.respondWith(
(async () => {
try {
// First, try to use the navigation preload response if it's supported.
const preloadResponse = await event.preloadResponse
if (preloadResponse) {
return preloadResponse
}
// Then try the network.
const networkResponse = await fetch(event.request)
return networkResponse
} catch (error) {
// catch is only triggered if an exception is thrown, which is likely
// due to a network error.
// If fetch() returns a valid HTTP response with a response code in
// the 4xx or 5xx range, the catch() will NOT be called.
console.log(
'Fetch failed; returning offline page instead.',
error
)
const cache = await caches.open(CACHE_NAME)
const cachedResponse = await cache.match(OFFLINE_PAGE_URL)
return cachedResponse
}
})()
)
}
})