TS conversion
This commit is contained in:
parent
75fa9d0844
commit
845c7f6612
44 changed files with 6225 additions and 611 deletions
93
.dist/service.js
Normal file
93
.dist/service.js
Normal file
|
@ -0,0 +1,93 @@
|
|||
function deleteoldcache() {
|
||||
caches.delete("cache");
|
||||
console.log("this ran :P");
|
||||
}
|
||||
async function putInCache(request, response) {
|
||||
console.log(request, response);
|
||||
const cache = await caches.open('cache');
|
||||
console.log("Grabbed");
|
||||
try {
|
||||
console.log(await cache.put(request, response));
|
||||
}
|
||||
catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
;
|
||||
console.log("test");
|
||||
let lastcache;
|
||||
self.addEventListener("activate", async (event) => {
|
||||
console.log("test2");
|
||||
checkCache();
|
||||
});
|
||||
async function checkCache() {
|
||||
if (checkedrecently) {
|
||||
return;
|
||||
}
|
||||
const promise = await caches.match("/getupdates");
|
||||
if (promise) {
|
||||
lastcache = await promise.text();
|
||||
}
|
||||
console.log(lastcache);
|
||||
fetch("/getupdates").then(async (data) => {
|
||||
const text = await data.clone().text();
|
||||
console.log(text, lastcache);
|
||||
if (lastcache !== text) {
|
||||
deleteoldcache();
|
||||
putInCache("/getupdates", data.clone());
|
||||
}
|
||||
checkedrecently = true;
|
||||
setTimeout(_ => { checkedrecently = false; }, 1000 * 60 * 30);
|
||||
});
|
||||
}
|
||||
var checkedrecently = false;
|
||||
function samedomain(url) {
|
||||
return new URL(url).origin === self.origin;
|
||||
}
|
||||
function isindexhtml(url) {
|
||||
console.log(url);
|
||||
if (new URL(url).pathname.startsWith("/channels")) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
async function getfile(event) {
|
||||
checkCache();
|
||||
if (!samedomain(event.request.url)) {
|
||||
return await fetch(event.request.clone());
|
||||
}
|
||||
const responseFromCache = await caches.match(event.request.url);
|
||||
console.log(responseFromCache, caches);
|
||||
if (responseFromCache) {
|
||||
console.log("cache hit");
|
||||
return responseFromCache;
|
||||
}
|
||||
if (isindexhtml(event.request.url)) {
|
||||
console.log("is index.html");
|
||||
const responseFromCache = await caches.match("/index.html");
|
||||
if (responseFromCache) {
|
||||
console.log("cache hit");
|
||||
return responseFromCache;
|
||||
}
|
||||
const responseFromNetwork = await fetch("/index.html");
|
||||
await putInCache("/index.html", responseFromNetwork.clone());
|
||||
return responseFromNetwork;
|
||||
}
|
||||
const responseFromNetwork = await fetch(event.request.clone());
|
||||
console.log(event.request.clone());
|
||||
await putInCache(event.request.clone(), responseFromNetwork.clone());
|
||||
try {
|
||||
return responseFromNetwork;
|
||||
}
|
||||
catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
}
|
||||
self.addEventListener('fetch', (event) => {
|
||||
try {
|
||||
event.respondWith(getfile(event));
|
||||
}
|
||||
catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue