-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtApp-service-worker.js
137 lines (130 loc) · 3.83 KB
/
tApp-service-worker.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
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
var version = 'v29::';
self.addEventListener("install", function(event) {
event.waitUntil(() => {
self.skipWaiting();
});
});
self.addEventListener("fetch", function(event) {
if (event.request.method !== 'GET') {
// Handle other requests such as POST here
return;
}
let fetchResponse;
/*
Add other edge cases based on event.request (for example, event.request.url == "/") here,
assign the return value to fetchResponse or use `return;` to skip the override
*/
fetchResponse = (async () => {
return new Promise((resolve, reject) => {
let url = event.request.url.split("#")[0];
let requestInit = indexedDB.open("tAppCache", 5);
let db;
function myFetch(page) {
return new Promise((resolve, reject) => {
fetch(page).then((response) => {
resolve(response);
}).catch(() => {
let res = new Response("", {
status: 500,
statusText: 'Service Unavailable',
headers: new Headers({
'Content-Type': 'text/html'
})
});
Object.defineProperty(res, "url", {value: url});
resolve(res);
});
});
}
requestInit.onupgradeneeded = function() {
db = requestInit.result;
if(!db.objectStoreNames.contains("cachedPages")) {
db.createObjectStore("cachedPages");
}
if(!db.objectStoreNames.contains("offlineStorage")) {
db.createObjectStore("offlineStorage");
}
}
requestInit.onsuccess = async function() {
db = requestInit.result;
function responseToJSON(request) {
let jsonRequest = {};
for(let property in request) {
if(property != "bodyUsed" && typeof request[property] != "object" && typeof request[property] != "function") {
jsonRequest[property] = request[property];
}
}
if(request.headers != null) {
jsonRequest.headers = {};
request.headers.forEach((value, key) => {
jsonRequest.headers[key] = value;
});
}
return jsonRequest;
}
function setCachedPage(fullPath, value) {
return new Promise((resolve, reject) => {
let request = db.transaction(["cachedPages"], "readwrite").objectStore("cachedPages").put(value, fullPath);
request.onerror = () => {
reject("tAppError: Persistent caching is not available in this browser.");
};
request.onsuccess = () => {
resolve(true);
}
});
}
function getCachedPage(fullPath) {
return new Promise((resolve, reject) => {
if(event.request.headers.get("tApp-Ignore-Cache") != "Ignore-Cache") {
let request = db.transaction(["cachedPages"], "readwrite").objectStore("cachedPages").get(fullPath);
request.onerror = (err) => {
myFetch(url).then((response) => {
resolve(response);
});
};
request.onsuccess = () => {
myFetch(url).then((response) => {
if(response.status === 200) {
response.clone().arrayBuffer().then((buffer) => {
setCachedPage(url, {
data: buffer,
cachedAt: new Date().getTime(),
response: responseToJSON(response)
});
});
}
});
if(request.result != null) {
let res = new Response(request.result.data, request.result.response);
Object.defineProperty(res, "url", {value: url});
resolve(res);
} else {
myFetch(url).then((response) => {
resolve(response);
});
}
};
} else {
myFetch(url).then((response) => {
resolve(response);
});
}
});
}
getCachedPage(url).then((response) => {
resolve(response);
});
};
requestInit.onerror = (err) => {
myFetch(url).then((response) => {
resolve(response);
});
}
});
})();
event.respondWith(fetchResponse);
});
self.addEventListener("activate", function(event) {
event.waitUntil(() => {
});
});