CVNSS commited on
Commit
440bd32
·
verified ·
1 Parent(s): 9f2b301

Create sw.js

Browse files
Files changed (1) hide show
  1. sw.js +39 -0
sw.js ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const CACHE = "tdhp-offline-v1";
2
+ const ASSETS = [
3
+ "./",
4
+ "./index.html",
5
+ "./manifest.webmanifest",
6
+ "./tu_dien_hoang_phe_clean.json"
7
+ ];
8
+
9
+ self.addEventListener("install", (event) => {
10
+ event.waitUntil(
11
+ caches.open(CACHE).then((c) => c.addAll(ASSETS)).then(() => self.skipWaiting())
12
+ );
13
+ });
14
+
15
+ self.addEventListener("activate", (event) => {
16
+ event.waitUntil(
17
+ caches.keys().then((keys) =>
18
+ Promise.all(keys.map((k) => (k === CACHE ? null : caches.delete(k))))
19
+ ).then(() => self.clients.claim())
20
+ );
21
+ });
22
+
23
+ self.addEventListener("fetch", (event) => {
24
+ const req = event.request;
25
+ event.respondWith(
26
+ caches.match(req).then((cached) => {
27
+ if (cached) return cached;
28
+ return fetch(req).then((fresh) => {
29
+ // cache lại các file tĩnh
30
+ const url = new URL(req.url);
31
+ if (ASSETS.includes(url.pathname.replace(/^\//, "./"))) {
32
+ const copy = fresh.clone();
33
+ caches.open(CACHE).then((c) => c.put(req, copy));
34
+ }
35
+ return fresh;
36
+ }).catch(() => cached);
37
+ })
38
+ );
39
+ });