-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
23 lines (18 loc) · 787 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
// Runs in Service Worker Scope (no access to DOM)
// Caches are shared across the origin and accessible to both SW scope and page scope
const CACHE_NAME = "v1";
const OFFLINE_URL = "/offline.html";
self.addEventListener("install", (event) => {
event.waitUntil(caches.open(CACHE_NAME).then(cache => cache.add(OFFLINE_URL)));
});
function fetch_handler(event) {
// Serve from the cache if present; add to the cache if not
event.respondWith(caches.match(event.request).then(found => found ? found :
caches.open(CACHE_NAME).then(cache =>
fetch(event.request).then(
res => cache.put(event.request, res.clone()).then(() => res),
err => (event.request.mode === 'navigate') ? caches.match(OFFLINE_URL) : undefined)
)
));
}
self.addEventListener("fetch", fetch_handler);