-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserverThread.js
More file actions
156 lines (123 loc) · 4.04 KB
/
serverThread.js
File metadata and controls
156 lines (123 loc) · 4.04 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
// Author : Zin Lin Htun
const staticCacheName = "site-static-v9";
const dynamicCacheName = "dynamic-v6";
//always change cache version
const asset = [
"./", // User ask without the display start up just on server
"./index.html",
"./init.js",
"./db.js",
"./auth.js",
"./ui.js",
"./search.html",
"./search.js",
"./filter.html",
"./filter.js",
"./about.html",
"./contact.html",
"./recipe.js",
"./profile.js",
"./recipe.html",
"./profile.html",
"./js/materialize.min.js",
"./js/ui.js",
"./pages/fallback.html",
"./css/materialize.min.css",
"./css/index.css",
"./img/dish.png",
"./img/beef.png",
"https://fonts.googleapis.com/icon?family=Material+Icons",
"https://fonts.gstatic.com/s/materialicons/v67/flUhRq6tzZclQEJ-Vdg-IuiaDsNc.woff2",
]//Assets that will be stored in the shell caches
/* We will need / to request things in same dir in web for the
server to notice */
//limit cache size
const limitCacheSize = (name, size)=>{
caches.open(name).then(cac =>
cac.keys().then(
keys=>{
if (keys.length>size && keys[0]!=="./js/db.js" ){
cac.delete(keys[0]).then(
limitCacheSize(name, size)
//Recalling to check the size
); //delete the first item
}
else if (keys.length>size && keys[0]=="./js/db.js" ){
cac.delete(keys[1]).then(
limitCacheSize(name, size)
//Recalling to check the size
); //delete the first item
}}
));
console.log("problem");
}
self.addEventListener("install",
(event)=> {
event.waitUntil(
caches.open(staticCacheName)
.then(
cache => {
cache.addAll(asset);
console.log("caching shell assets;;;;;;");
})
);
//Create if not exist in native browser
})
//self = this module
//self make clear it's not part of the document
//so it would run on a different thread compared with
//a norm of a DOM Js File...
//Activate the serviceWorker to use it
self.addEventListener("activate", eve =>{
// WE will delete old cache
eve.waitUntil(
//Destroy old cache
caches.keys().then(keys =>{ //Asyn - Await //
//keys get all the caches...
//console.log(keys);
return Promise.all(
keys.filter(key=> key!== staticCacheName
&& key!== dynamicCacheName).map(
key => caches.delete(key)
)
);
})
);
// console.log("Service Worker Has Been activated...");
// console.log("Service Worker Has Been activated of course..");
});
//Listen for fetch event when browser requests
self.addEventListener("fetch", (eve)=>{
//console.log("event", eve);
if (eve.request.url.indexOf("firestore.googleapis.com") === -1 &&
eve.request.url.indexOf("/g/collect") === -1)
{
eve.respondWith(
caches.match(eve.request).then(cac =>{
return cac || fetch(eve.request).then(
//caching what user browse
//fetch is a promise Future
Fres =>{
return caches.open(dynamicCacheName).then(
cac =>{
cac.put(eve.request.url, Fres.clone());
limitCacheSize(dynamicCacheName, 15);
return Fres; //MEW
//FRRR
}
)}
)
//if null return the actual fetch
}).catch(
()=>{
//we dun pass the error since we won't use it
if (eve.request.url.indexOf(".html") > -1){
return caches.match("./pages/fallback.html");
}
}
)
);
}
});
//This would maybe catch the service and allows us to use it offline
//This is also needed for the request banner...