-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyWorker.js
86 lines (63 loc) · 1.85 KB
/
myWorker.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
84
85
86
self.addEventListener("install",function(event){
console.log("cache started");
var static_Name="Static-Cache";
var files_to_cache=['/myJs.js','/style.css','/myHTML.html','/'];
self.skipWaiting();
event.waitUntil(
caches.open(static_Name).then(function(cach){
return cach.addAll(files_to_cache);
})
);
});
self.addEventListener("fetch",function(event){
if(event.request.url.startsWith("https://api.github.com/")){
caches.open("dynamic-data-2").then(function(my_cach){
my_cach.add(event.request.url);
});
}
/*
*alternative method
fetch(event.request).then(function(response){
caches.open("dynamic-data-2").then(function(my_cach){
my_cach.put(event.request.url,response);
console.log("Response has been added successfully");
})
}).catch(function(err){
}) ;*/
/*
event.respondWith(
caches.open('dynamic-data-2').then(function(my_cache) {
console.log("This lines run");
return fetch(event.request).then(function(response) {
console.log(event.request.url);
my_cache.put(event.request, response.clone());
return response;
});
})
);
*/
else{
var static_Name="Static-Cache";
event.respondWith(
caches.match(event.request).then(function(response){
return response||fetch(event.request);
}));
}
}
);
/*
self.addEventListener("fetch",function(event){
if(event.request.url.startsWith("https://api.github.com/")){
event.respondWith(
fetch(event.request).then(function(response){
return caches.open("dynamic-cache").then(function(cach){
cach.add(response.clone());
return response;
});
return response;
})
);
}
}
);
*/