-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathsw.js
More file actions
60 lines (57 loc) · 1.74 KB
/
sw.js
File metadata and controls
60 lines (57 loc) · 1.74 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const CACHE_NAME = 'v1';
const STATIC_CACHE_URLS = [];
const cache = (request, response) => {
if (response.type === 'error' || response.type === 'opaque') {
return Promise.resolve(); // do not put in cache network errors
}
return caches
.open(CACHE_NAME)
.then(cache => cache.put(request, response.clone()));
};
const normaliseRequest = (request) => {
if (request.headers.get('Range')) {
return new Request(request, {
headers: {
...request.headers,
Range: null
}
})
}
return request;
};
self.addEventListener('install', event => {
console.log('Service Worker installing.');
event.waitUntil(
caches.open(CACHE_NAME).then(cache => cache.addAll(STATIC_CACHE_URLS))
);
});
self.addEventListener('fetch', event => {
// Network-First Cache-Fallback Strategy
// Open the cache
event.respondWith(caches.open(CACHE_NAME).then((cache) => {
// Go to the network first
return fetch(event.request.url).then((fetchedResponse) => {
cache.put(event.request, fetchedResponse.clone());
return fetchedResponse;
}).catch(() => {
// If the network is unavailable, get
return cache.match(event.request.url);
});
}));
});
self.addEventListener('activate', event => {
// delete any unexpected caches
event.waitUntil(
caches
.keys()
.then(keys => keys.filter(key => key !== CACHE_NAME))
.then(keys =>
Promise.all(
keys.map(key => {
console.log(`Deleting cache ${key}`);
return caches.delete(key);
})
)
)
);
});