-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
25 lines (23 loc) · 952 Bytes
/
sw.js
File metadata and controls
25 lines (23 loc) · 952 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
const CACHE_NAME = 'bang-cache-v1';
const ASSETS = ['/', '/index.html', '/manifest.json'];
self.addEventListener('install', (e) => {
e.waitUntil(caches.open(CACHE_NAME).then((cache) => cache.addAll(ASSETS)));
});
self.addEventListener('fetch', (e) => {
// Only intercept the HTML page request, ignore external bang fetches
const url = new URL(e.request.url);
if (url.origin === location.origin) {
e.respondWith(
caches.match(e.request).then((cachedResponse) => {
const networkFetch = fetch(e.request).then((response) => {
caches.open(CACHE_NAME).then((cache) => {
cache.put(e.request, response.clone());
});
return response;
});
// Return cached version immediately if available, else fetch
return cachedResponse || networkFetch;
})
);
}
});