Bintang Fajar Julio commited on
Commit
5518c61
1 Parent(s): cc56a85
Files changed (2) hide show
  1. static/manifest.json +4 -2
  2. static/service-worker.js +47 -26
static/manifest.json CHANGED
@@ -2,14 +2,16 @@
2
  "name": "Skripsi JTIK",
3
  "short_name": "SkripsiJTIK",
4
  "start_url": "/",
5
- "display": "standalone",
6
  "background_color": "#ffffff",
 
 
7
  "theme_color": "#0f8a9a",
8
  "icons": [
9
  {
10
  "src": "/static/images/favicon.png",
 
11
  "type": "image/png",
12
- "sizes": "192x192"
13
  }
14
  ]
15
  }
 
2
  "name": "Skripsi JTIK",
3
  "short_name": "SkripsiJTIK",
4
  "start_url": "/",
 
5
  "background_color": "#ffffff",
6
+ "description": "Web App for Skripsi JTIK PNJ",
7
+ "display": "fullscreen",
8
  "theme_color": "#0f8a9a",
9
  "icons": [
10
  {
11
  "src": "/static/images/favicon.png",
12
+ "sizes": "512x512",
13
  "type": "image/png",
14
+ "purpose": "any maskable"
15
  }
16
  ]
17
  }
static/service-worker.js CHANGED
@@ -1,32 +1,53 @@
1
- const CACHE_NAME = "my-cache";
2
- const OFFLINE_URL = "/static/offline.html";
 
 
3
 
4
- self.addEventListener("install", (event) => {
5
- event.waitUntil(
6
- caches.open(CACHE_NAME).then((cache) => {
7
- return cache.addAll([
8
- "/",
9
- "/static/css/portal.css",
10
- "/static/js/app.js",
11
- "/static/images/favicon.png",
12
- OFFLINE_URL,
13
- ]);
14
- })
15
- );
16
  });
17
 
18
- self.addEventListener("fetch", (event) => {
19
- if (event.request.mode === "navigate") {
20
- event.respondWith(
21
- fetch(event.request).catch(() => {
22
- return caches.match(OFFLINE_URL);
23
- })
24
- );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  } else {
26
- event.respondWith(
27
- caches.match(event.request).then((response) => {
28
- return response || fetch(event.request);
29
- })
30
- );
 
 
 
 
 
 
 
31
  }
32
  });
 
1
+ const preLoad = async function () {
2
+ const cache = await caches.open("offline");
3
+ return await cache.addAll(filesToCache);
4
+ };
5
 
6
+ self.addEventListener("install", function (event) {
7
+ event.waitUntil(preLoad());
 
 
 
 
 
 
 
 
 
 
8
  });
9
 
10
+ const filesToCache = ["/", "/static/offline.html"];
11
+
12
+ const checkResponse = function (request) {
13
+ return new Promise(function (fulfill, reject) {
14
+ fetch(request).then(function (response) {
15
+ if (response.status !== 404) {
16
+ fulfill(response);
17
+ } else {
18
+ reject();
19
+ }
20
+ }, reject);
21
+ });
22
+ };
23
+
24
+ const addToCache = async function (request) {
25
+ if (request.url.startsWith("chrome-extension://")) {
26
+ return Promise.resolve();
27
+ }
28
+
29
+ const cache = await caches.open("offline");
30
+ const response = await fetch(request);
31
+ return await cache.put(request, response);
32
+ };
33
+
34
+ const returnFromCache = async function (request) {
35
+ const cache = await caches.open("offline");
36
+ const matching = await cache.match(request);
37
+ if (!matching || matching.status === 404) {
38
+ return cache.match("offline.html");
39
  } else {
40
+ return matching;
41
+ }
42
+ };
43
+
44
+ self.addEventListener("fetch", function (event) {
45
+ event.respondWith(
46
+ checkResponse(event.request).catch(function () {
47
+ return returnFromCache(event.request);
48
+ })
49
+ );
50
+ if (!event.request.url.startsWith("http")) {
51
+ event.waitUntil(addToCache(event.request));
52
  }
53
  });