improve fetch intercept

This commit is contained in:
Seth Trowbridge 2025-08-23 09:48:43 -04:00
parent 89908827fb
commit a280e5059f
2 changed files with 41 additions and 41 deletions

20
app.js
View File

@ -23,12 +23,11 @@ const H =(type, attributes={}, ...children)=> {
const button = H("button"); const button = H("button");
const listRoom = H("ul"); const listRoom = H("ul");
async function getFolderHandle(dirHandle) /** @type {(handle:boolean)=>Promise<void>} */
async function ReloadAndRedraw(handle)
{ {
try if(handle)
{ {
const workingFolder = await dirHandle.getDirectoryHandle("room");
const module = await import("./data/room.js"); const module = await import("./data/room.js");
const rooms = module.default(); const rooms = module.default();
button.innerText = "change directory"; button.innerText = "change directory";
@ -50,27 +49,24 @@ async function getFolderHandle(dirHandle)
) )
); );
}) })
return true;
} }
catch (err) else
{ {
button.innerText = "select directory"; button.innerText = "select directory";
console.error("Folder selection cancelled or failed:", err);
return false;
} }
} }
let handle = false; let handle = false;
handle = await FSHandle.getDirectoryHandle(); handle = await FSHandle.getDirectoryHandle();
await getFolderHandle(handle); await ReloadAndRedraw(handle);
button.addEventListener("click", async()=> button.addEventListener("click", async()=>
{ {
const directory = await globalThis.showDirectoryPicker(); const directory = await globalThis.showDirectoryPicker();
await FSHandle.setDirectoryHandle(directory); await FSHandle.setDirectoryHandle(directory);
await getFolderHandle(directory); handle = await FSHandle.getDirectoryHandle();
await ReloadAndRedraw(handle);
}); });
document.body.appendChild(button); document.body.appendChild(button);

View File

@ -8,43 +8,47 @@ let handle = false;
async function Interceptor(event) async function Interceptor(event)
{ {
const url = new URL(event.request.url); const url = new URL(event.request.url);
const pathname = url.pathname.substring(1) const pathname = url.pathname.substring(1);
const extension = pathname.substring(pathname.lastIndexOf('.') + 1); const parts = pathname.split("/");
// Intercept only JavaScript files if(parts[0] == "data" || parts[0] == "room")
if ( extension == "js" || extension == "json" )
{ {
console.log('[Service Worker] Intercepted JS request:', url.href); const extension = pathname.substring(pathname.lastIndexOf('.') + 1);
if(!handle) // Intercept only JavaScript files
if ( extension == "js" || extension == "json" )
{ {
handle = await FSAccess.getDirectoryHandle(); if(!handle)
}
if(handle)
{
try
{ {
const parts = pathname.split("/"); handle = await FSAccess.getDirectoryHandle();
let filePointer = handle; }
for(let i=0; i<parts.length-1; i++) if(handle)
{
try
{ {
filePointer = await filePointer.getDirectoryHandle(parts[i], {create: false}); let filePointer = handle;
} for(let i=0; i<parts.length-1; i++)
filePointer = await filePointer.getFileHandle(parts[parts.length-1], {create: false}); {
const file = await filePointer.getFile(); filePointer = await filePointer.getDirectoryHandle(parts[i], {create: false});
const content = await file.text();
return new Response(content, {
headers: {
'Content-Type': 'application/javascript',
'Cache-Control': 'no-cache'
} }
}); filePointer = await filePointer.getFileHandle(parts[parts.length-1], {create: false});
} const file = await filePointer.getFile();
catch(e) const content = await file.text();
{
return fetch(event.request);
}
return new Response(content, {
headers: {
'Content-Type': 'application/javascript',
'Cache-Control': 'no-cache'
}
});
}
catch(e)
{
return fetch(event.request);
}
}
} }
} }
else else
{ {