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,13 +8,15 @@ 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("/");
if(parts[0] == "data" || parts[0] == "room")
{
const extension = pathname.substring(pathname.lastIndexOf('.') + 1);
// Intercept only JavaScript files // Intercept only JavaScript files
if ( extension == "js" || extension == "json" ) if ( extension == "js" || extension == "json" )
{ {
console.log('[Service Worker] Intercepted JS request:', url.href);
if(!handle) if(!handle)
{ {
handle = await FSAccess.getDirectoryHandle(); handle = await FSAccess.getDirectoryHandle();
@ -23,7 +25,6 @@ async function Interceptor(event)
{ {
try try
{ {
const parts = pathname.split("/");
let filePointer = handle; let filePointer = handle;
for(let i=0; i<parts.length-1; i++) for(let i=0; i<parts.length-1; i++)
{ {
@ -32,6 +33,7 @@ async function Interceptor(event)
filePointer = await filePointer.getFileHandle(parts[parts.length-1], {create: false}); filePointer = await filePointer.getFileHandle(parts[parts.length-1], {create: false});
const file = await filePointer.getFile(); const file = await filePointer.getFile();
const content = await file.text(); const content = await file.text();
return new Response(content, { return new Response(content, {
headers: { headers: {
'Content-Type': 'application/javascript', 'Content-Type': 'application/javascript',
@ -46,6 +48,8 @@ async function Interceptor(event)
} }
} }
}
else else
{ {
return fetch(event.request); return fetch(event.request);