-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
39 lines (37 loc) · 1.03 KB
/
Copy pathsw.js
File metadata and controls
39 lines (37 loc) · 1.03 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
const cacheName = 'my-cache';
const offlinePage = '/offline.html';
const filesList = [
offlinePage
];
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(cacheName)
// Add your file to cache
.then( (cache) => {
return cache.addAll(filesList);
})
// Tell SW to end 'waiting' state
.then(() => self.skipWaiting())
);
})
self.addEventListener('activate', (event) => {
event.waitUntil(
// Tell browser to use this service worker and not outdated one
self.clients.claim()
);
})
self.addEventListener('fetch', (event) => {
// If we don't have internet connection and it's a navigation request
if (!navigator.onLine && event.request.mode === 'navigate') {
event.respondWith( caches.open(cacheName)
.then( (cache) => {
// If we find our offline page in cache, respond with it
return cache.match(offlinePage)
.then( (response) => response);
})
);
} else {
// Return null let browser do his normal behavior
return;
}
})