Add Start/End work transitions, capture field, and PWA support
This commit is contained in:
53
static/sw.js
Normal file
53
static/sw.js
Normal file
@ -0,0 +1,53 @@
|
||||
const CACHE = "orgweb-v1";
|
||||
const PRECACHE = [
|
||||
"/",
|
||||
"/static/style.css",
|
||||
"/static/manifest.webmanifest",
|
||||
"/static/icon-192.png",
|
||||
"/static/icon-512.png",
|
||||
"/static/icon-512-maskable.png",
|
||||
];
|
||||
|
||||
self.addEventListener("install", (event) => {
|
||||
event.waitUntil(
|
||||
caches
|
||||
.open(CACHE)
|
||||
.then((cache) => cache.addAll(PRECACHE))
|
||||
.then(() => self.skipWaiting())
|
||||
);
|
||||
});
|
||||
|
||||
self.addEventListener("activate", (event) => {
|
||||
event.waitUntil(
|
||||
caches
|
||||
.keys()
|
||||
.then((keys) =>
|
||||
Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k)))
|
||||
)
|
||||
.then(() => self.clients.claim())
|
||||
);
|
||||
});
|
||||
|
||||
self.addEventListener("fetch", (event) => {
|
||||
const url = new URL(event.request.url);
|
||||
if (url.origin !== location.origin) return;
|
||||
|
||||
if (event.request.mode === "navigate") {
|
||||
event.respondWith(
|
||||
fetch(event.request).catch(() => caches.match("/"))
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
event.respondWith(
|
||||
caches.match(event.request).then(
|
||||
(cached) =>
|
||||
cached ||
|
||||
fetch(event.request).then((resp) => {
|
||||
const copy = resp.clone();
|
||||
caches.open(CACHE).then((cache) => cache.put(event.request, copy));
|
||||
return resp;
|
||||
})
|
||||
)
|
||||
);
|
||||
});
|
||||
Reference in New Issue
Block a user