-
Notifications
You must be signed in to change notification settings - Fork 3
/
serviceworker.js
59 lines (54 loc) · 2.48 KB
/
serviceworker.js
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
console.log("registered")
self.addEventListener('fetch', function(event) {
const url = new URL(event.request.url);
if (event.request.method === 'POST' && url.pathname === '/maze/receive-share') {
// redirect the user to main page and process the recieved files
event.respondWith(Response.redirect('/maze/index.html'));
event.waitUntil(async function () {
// get the client obj to post message later
const client = await self.clients.get(event.resultingClientId);
// get the data from the recieved request
const data = await event.request.formData();
// get all the files from recieved data
let files = [];
data.forEach(z=> {
files.push(z);
});
// send all the files via postmessage to the application
client.postMessage({ files });
}());
return;
}else {
event.respondWith(
(async ()=> {
try {
// Always try the network first.
const networkResponse = await fetch(event.request);
return networkResponse;
} catch (error) {
console.log("Fetch failed; returning offline page instead.", error);
// open the cache storage
const cache = await caches.open("maze-pwa");
// for each resource requested, serve them response from cache. (for html send html, for js send js, etc)
const cachedResponse = await cache.match(event.request, {ignoreVary:true});
//console.log(cache, cachedResponse, event.request)
// return the response
return cachedResponse;
}
})()
);
}
});
// caching all the resources on install
const urlsToCache = ["/maze/", "/maze/styles/mstyle.css", "/maze/styles/mg.css", "/maze/scripts/script.js", "/maze/packages/index.js",
"https://cdn.jsdelivr.net/gh/0-harshit-0/Utility-HTML5Canvas@master/src/shapes.min.js",
"https://cdn.jsdelivr.net/gh/0-harshit-0/Utility-HTML5Canvas@master/src/vector.min.js",
"/maze/assets/SometypeMono-Regular.woff2", "/maze/assets/maze-16.png", "/maze/assets/maze-512.png", "/maze/manifest.json"];
self.addEventListener("install", (event) => {
console.log("caching");
event.waitUntil(
caches.open("maze-pwa").then(cache => {
return cache.addAll(urlsToCache);
})
);
});