41 lines
1.1 KiB
JavaScript
41 lines
1.1 KiB
JavaScript
import * as FSAccess from "./store-directory-handle.js";
|
|
|
|
self.addEventListener('install', ()=> self.skipWaiting()); // Activate worker immediately);
|
|
self.addEventListener('activate', ()=> self.clients.claim()); // Become available to all pages);
|
|
self.addEventListener('fetch', (event) =>event.respondWith(Interceptor(event)));
|
|
|
|
async function Interceptor(event)
|
|
{
|
|
const url = new URL(event.request.url);
|
|
const pathname = url.pathname.substring(1);
|
|
const parts = pathname.split("/");
|
|
|
|
if(parts[0] == "data" || parts[0] == "room")
|
|
{
|
|
console.log("intercept:", pathname)
|
|
const handle = await FSAccess.getDirectoryHandle();
|
|
if(handle)
|
|
{
|
|
const file = await FSAccess.drilldown(handle, parts);
|
|
if(file)
|
|
{
|
|
const content = await file.text();
|
|
return new Response(content, {
|
|
headers: {
|
|
'Content-Type': 'application/javascript',
|
|
'Cache-Control': 'no-cache'
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
console.log("couldnt find:", pathname);
|
|
return new Response("404", {status:404});
|
|
|
|
}
|
|
else
|
|
{
|
|
return fetch(event.request);
|
|
}
|
|
}
|