-
Notifications
You must be signed in to change notification settings - Fork 32
/
swdb.js
84 lines (80 loc) · 2.61 KB
/
swdb.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
self.addEventListener("install", event => {
const parts = self.location.search.substring(1).split("="),
params = {};
for(let i=0;i<parts.length;i+=2) {
params[parts[i]]=parts[i+1];
}
event.waitUntil(Promise.all([caches.open(params.name),caches.open("website")]));
});
async function put(cachename,key,response) {
if(response.ok) {
const cache = await caches.open(cachename);
cache.put(key,response);
}
}
async function del(cachename,key) {
const cache = await caches.open(cachename);
cache.delete(key);
}
self.addEventListener('fetch', event => {
const parts = event.request.url.split("/"),
protocol = parts.shift(),
empty = parts.shift(),
host = parts.shift(),
swdb = parts.shift(),
cachename = parts.shift(),
deletecache = parts.length===0;
if(swdb!=="swdb") {
event.respondWith(caches.open("website").then(async cache => {
let response = await cache.match(event.request.url);
if(response) {
//fetch(event.request).then(refreshed => {
// setTimeout(put,0,cachename,event.request.url,refreshed.clone());
//});
return response;
}
response = await fetch(event.request);
if(response.ok) cache.put(event.request.url,response.clone());
return response;
}))
} else if(event.request.method==="PUT") {
//console.log("put");
const promise = event.request.json().then(async json => {
const data = JSON.stringify(json),
response = new Response(data,{headers:{"Content-Type":"application/json; ; charset=utf-8","Content-Length":data.length}}); setTimeout(put,0,cachename,event.request.url,response.clone());
//await put(cachename,event.request.url,response.clone());
return response;
});
event.respondWith(promise.then(response => response))
} else if(event.request.method==="GET"){
event.respondWith(
caches.open(cachename).then(async cache => {
const response = await cache.match(event.request.url);
if(response) return response;
return new Response("null");
}).catch(e => {
return new Response("null");
})
)
} else if(event.request.method==="DELETE") {
//console.log("delete");
if(deletecache) {
const promise = caches.delete(cachename).then(found => new Response(`${found}`));
event.respondWith(promise.then(response => response));
} else {
event.respondWith(
caches.open(cachename).then(async cache => {
const deleted = await cache.match(event.request.url);
if(deleted) {
setTimeout(del,0,cachename,event.request.url);
return new Response("true");
}
return new Response("false");
}).catch(e => {
return new Response("false");
})
)
}
}
});