-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
executable file
·33 lines (33 loc) · 1023 Bytes
/
Copy pathsw.js
File metadata and controls
executable file
·33 lines (33 loc) · 1023 Bytes
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
const cacheName = 'static-v1';
self.addEventListener('activate', event => {
event.waitUntil(
self.registration.navigationPreload?.enable()
);
});
self.addEventListener('fetch', event => {
if (event.request.method !== 'GET') return;
event.respondWith((async () => {
const cache = await caches.open(cacheName);
// Use preload for navigations
const preload = await event.preloadResponse;
if (preload) {
cache.put(event.request, preload.clone());
return preload;
}
// Return cached, update in background
const cached = await cache.match(event.request);
if (cached) {
event.waitUntil(
fetch(event.request)
.then(r => r.ok && cache.put(event.request, r))
.catch(() => {})
);
return cached;
}
// Fetch and cache
const response = await fetch(event.request);
if (response.ok) cache.put(event.request, response.clone());
return response;
})());
});
self.addEventListener('install', () => self.skipWaiting());