Compare commits

..

3 commits

Author SHA1 Message Date
Pagwin
8298e7d098 keyPath added 2025-09-03 16:42:39 -04:00
Pagwin
d9541fb2b0 didn't realize transaction needed readwrite 2025-09-03 16:37:30 -04:00
Pagwin
20ea7787dc debugging demo 4 2025-09-03 16:29:04 -04:00
3 changed files with 36 additions and 35 deletions

View file

@ -70,10 +70,10 @@
await navigator.serviceWorker.register("sw.js"); await navigator.serviceWorker.register("sw.js");
</script> </script>
<p>Here is some example text</p> <p>Here is some example text</p>
<form action="/light-dark-toggle" method="get"> <form action="./light-dark-toggle" method="get">
<input id="full-demo" type="submit" value="Fully enhanced demo"> <input id="full-demo" type="submit" value="Fully enhanced demo">
</form> </form>
<form action="/light-dark-toggle" method="get"> <form action="./light-dark-toggle" method="get">
<input id="min-demo" type="submit" value="No javascript simulation"> <input id="min-demo" type="submit" value="No javascript simulation">
</form> </form>
</body> </body>

View file

@ -9,6 +9,7 @@ self.addEventListener("fetch", event =>{
const resp3 = resp2.clone(); const resp3 = resp2.clone();
const body = await resp2.text(); const body = await resp2.text();
const url = new URL(event.request.url); const url = new URL(event.request.url);
console.log(url.pathname);
if(url.pathname.endsWith("light-dark-toggle")){ if(url.pathname.endsWith("light-dark-toggle")){
return await handle_redirect(event.request); return await handle_redirect(event.request);
} }
@ -24,13 +25,13 @@ async function handle_redirect(req){
const up_promise = new Promise((res)=>{ const up_promise = new Promise((res)=>{
db_req.onupgradeneeded = (event) => { db_req.onupgradeneeded = (event) => {
const db = event.target.result; const db = event.target.result;
IDB_cond_create(db, "light-dark-store", {}).then(res); IDB_cond_create(db, "light-dark-store", {keyPath: 'id'}).then(res);
} }
}); });
const suc_promise = new Promise((res)=>{ const suc_promise = new Promise((res)=>{
db_req.onsuccess = (event) => { db_req.onsuccess = (event) => {
const db = event.target.result; const db = event.target.result;
const transaction = db.transaction("light-dark-store"); const transaction = db.transaction("light-dark-store", "readwrite");
transaction.oncomplete = res; transaction.oncomplete = res;
transaction.objectStore("light-dark-store", "readwrite"); transaction.objectStore("light-dark-store", "readwrite");
@ -88,7 +89,7 @@ async function handle_html(req, resp, body){
const transaction = db.transaction("light-dark-store"); const transaction = db.transaction("light-dark-store");
transaction.oncomplete = res; transaction.oncomplete = res;
const obj_store = transaction.objectStore("light-dark-store", "read"); const obj_store = transaction.objectStore("light-dark-store", "readonly");
const grab = obj_store.get(1); const grab = obj_store.get(1);
grab.onsuccess = (event) => {res(!!event.result)}; grab.onsuccess = (event) => {res(!!event.result)};
}; };

60
sw.js
View file

@ -1,30 +1,30 @@
"use strict"; //"use strict";
//
// used so I can prompt updates to the service worker //// used so I can prompt updates to the service worker
const version = "22-01-2025"; //const version = "22-01-2025";
//
const cache = new Cache(); //const cache = new Cache();
//
self.addEventListener("install", (event) => { //self.addEventListener("install", (event) => {
// can't add /static/* due to demos living there // // can't add /static/* due to demos living there
cache.addAll([ // cache.addAll([
"/", // "/",
"/index.html", // "/index.html",
"/static/css/*", // "/static/css/*",
"/static/js/*", // "/static/js/*",
"/static/images/*", // "/static/images/*",
"/static/video/*" // "/static/video/*"
]); // ]);
}); //});
//
async function handleCache(event) { //async function handleCache(event) {
const cache_value = await cache.match(event.request); // const cache_value = await cache.match(event.request);
if (cache_value === undefined) { // if (cache_value === undefined) {
return fetch(event.request); // return fetch(event.request);
} // }
return cache_value; // return cache_value;
} //}
//
self.addEventListener("fetch", async (event) => { //self.addEventListener("fetch", async (event) => {
event.respondWith(handleCache(event)); // event.respondWith(handleCache(event));
}); //});