-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
30 lines (27 loc) · 863 Bytes
/
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
// sw.js
const CACHE_NAME = 'offline-cache-v1.0.4';
const urlParams = new URLSearchParams(location.search);
const OFFLINE_URL = urlParams.get('file');
self.addEventListener('install', event => {
console.log('Service Worker: Install event');
event.waitUntil(
caches.open(CACHE_NAME)
.then(cache => {
console.log('Service Worker: Caching offline page');
return cache.addAll([OFFLINE_URL]);
})
);
});
self.addEventListener('activate', event => {
console.log('Service Worker: Activate event');
event.waitUntil(self.clients.claim());
});
self.addEventListener('fetch', event => {
console.log('Service Worker: Fetch event for', event.request.url);
event.respondWith(
fetch(event.request)
.catch(() => caches.match(event.request)
.then(response => response || caches.match(OFFLINE_URL))
)
);
});