sw.js
1132 bytes
1const CACHE_NAME = "ytrssil-v4";
2const ASSETS_TO_CACHE = [
3 "/",
4 "/assets/vendor/bootstrap.min.css",
5 "/assets/vendor/bootstrap-icons.min.css",
6 "/assets/vendor/bootstrap.bundle.min.js",
7 "/assets/vendor/datastar.js",
8 "/assets/vendor/fuse.js",
9 "/assets/index.js",
10 "/assets/ytrssil.png",
11 "/assets/manifest.json",
12];
13
14self.addEventListener("install", (event) => {
15 event.waitUntil(
16 caches.open(CACHE_NAME).then((cache) => {
17 return cache.addAll(ASSETS_TO_CACHE);
18 }),
19 );
20 self.skipWaiting();
21});
22
23self.addEventListener("activate", (event) => {
24 event.waitUntil(
25 caches.keys().then((cacheNames) => {
26 return Promise.all(
27 cacheNames.map((cacheName) => {
28 if (cacheName !== CACHE_NAME) {
29 return caches.delete(cacheName);
30 }
31 }),
32 );
33 }),
34 );
35 self.clients.claim();
36});
37
38self.addEventListener("fetch", (event) => {
39 if (event.request.method !== "GET") return;
40
41 const url = new URL(event.request.url);
42 if (url.pathname.match(/^\/videos\/[^/]+\/file$/)) {
43 return;
44 }
45
46 event.respondWith(
47 caches.match(event.request).then((response) => {
48 return response || fetch(event.request);
49 }),
50 );
51});