-
-
Notifications
You must be signed in to change notification settings - Fork 139
Expand file tree
/
Copy pathservice-worker.js
More file actions
96 lines (89 loc) · 2.58 KB
/
Copy pathservice-worker.js
File metadata and controls
96 lines (89 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
const APP_VERSION = "v1.1.6";
const CACHE_VERSION = APP_VERSION;
const CACHE_NAME = `mazanoke-cache-${CACHE_VERSION}`;
const urlsToCache = [
"/",
"/index.html",
"/assets/css/fonts.css",
"/assets/css/variables.css",
"/assets/css/style.css",
"/assets/vendor/browser-image-compression.js",
"/assets/vendor/heic-to.js",
"/assets/vendor/libheif.js",
"/assets/vendor/ico.js",
"/assets/vendor/jszip.js",
"/assets/js/global.js",
"/assets/js/utilities.js",
"/assets/js/helpers.js",
"/assets/js/ui.js",
"/assets/js/compression.js",
"/assets/js/download.js",
"/assets/js/events.js",
"/assets/images/android-chrome-192x192.png",
"/assets/images/android-chrome-512x512.png",
"/assets/images/apple-touch-icon.png",
"/assets/images/symbol-192x192.png",
];
self.addEventListener("install", (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => cache.addAll(urlsToCache)),
);
});
self.addEventListener("fetch", (event) => {
const requestUrl = new URL(event.request.url);
if (["chrome-extension:", "file:", "about:"].includes(requestUrl.protocol)) {
return;
}
event.respondWith(
caches.match(event.request).then((cachedResponse) => {
if (cachedResponse) {
// Serve cached content
fetch(event.request)
.then((networkResponse) => {
if (networkResponse && networkResponse.ok) {
caches.open(CACHE_NAME).then((cache) => {
cache.put(event.request, networkResponse);
});
}
})
.catch(() => {});
return cachedResponse;
}
// If cache does not exist, fetch from network
return fetch(event.request).then((networkResponse) => {
if (!networkResponse || !networkResponse.ok) {
return networkResponse;
}
const responseToCache = networkResponse.clone();
caches.open(CACHE_NAME).then((cache) => {
cache.put(event.request, responseToCache);
});
return networkResponse;
});
}),
);
});
self.addEventListener("activate", (event) => {
event.waitUntil(
caches
.keys()
.then((cacheNames) => {
return Promise.all(
cacheNames.map((cacheName) => {
if (
cacheName.startsWith("mazanoke-cache-") &&
cacheName !== CACHE_NAME
) {
return caches.delete(cacheName);
}
}),
);
})
.then(() => self.clients.claim()),
);
});
self.addEventListener("message", (event) => {
if (event.data === "SKIP_WAITING") {
self.skipWaiting();
}
});