-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice-worker.js
More file actions
160 lines (102 loc) · 5.3 KB
/
service-worker.js
File metadata and controls
160 lines (102 loc) · 5.3 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//1
console.log('[ sw ]','running');
var cache_name = 'runtime-cache-v1';
var ttl = 5000;
var timestamps = new Map();
var ext;
var html_loader;
self.addEventListener('install',e=>{
console.log('[ sw ]','install');
self.skipWaiting();
});
self.addEventListener('activate',e=>{
console.log('[ sw ]','activate');
e.waitUntil((async()=>{
var keys = await caches.keys();
await Promise.all(keys.map(key=>{
if(key!==cache_name){
return caches.delete(key);
}
}));
await cache_purge();
self.clients.claim();
})());
});
self.addEventListener('fetch',e=>{
console.log('[ sw ]','fetch');
var {request} = e;
if(request.method!=='GET'){
return;
}
e.respondWith(cache_request(e,request));
});
self.addEventListener('message',({data})=>{
console.log('[ sw ]','message',data);
if(data.type=='load')load(data);
});
//:
async function cache_request(e,request){
console.log('[ sw ]','cache_request');
var client;
var url;
if(e.clientId){
client = await self.clients.get(e.clientId);
//console.log(client);
url = client?.url;
}
console.log('[ sw ]','url',url,request.url);
var cache = await caches.open(cache_name);
var cached = await cache.match(request);
if(url){
if(url.includes('/html-editor')){
var now = Date.now();
var last = timestamps.get(request.url)||0;
if(cached){
if(now-last<ttl){
console.log('[ sw ]','cache hit');
return cached;
}
timestamps.delete(request.url);
cache.delete(request);
}
}
}
var response = await fetch(request);
cache.put(request,response.clone());
timestamps.set(request.url,now);
return response;
}//cache_request
async function cache_purge(){
console.log('[ sw ]','cache_purge');
var cache = await caches.open(cache_name);
var keys = await cache.keys();
var now = Date.now();
for(var request of keys){
var last = timestamps.get(request.url)||0;
if(now-last>ttl){
await cache.delete(request);
timestamps.delete(request.url);
}
}//for
}//purge
function load(data){
}//load
function cloud_loader(request){
var url = request.url;
var nodename;
var i = url.indexOf('?');
if(i!=-1){
nodename = url.slice(i+1);
nodename = decodeURI(nodename);
//df && console.log('url ?',nodename);
}
if(nodename){
var txt = html_loader;
var i = txt.indexOf('/* params */');
i = txt.indexOf('\n',i)+1;
txt = txt.slice(0,i)+`var nodename='${nodename}';`+txt.slice(i+1);
}
var opts = {};
var response = new Response(txt2,opts);
return response;
}//cloud_loader