Service worker cleanup
parent
f39ab26cea
commit
98bebf3cb8
|
@ -74,75 +74,72 @@ self.addEventListener("fetch", (event: any) => {
|
||||||
const cacheKey = precacheFiles[url.pathname] || event.request.url;
|
const cacheKey = precacheFiles[url.pathname] || event.request.url;
|
||||||
|
|
||||||
event.respondWith(
|
event.respondWith(
|
||||||
// Try the static (client) file cache first
|
(async () => {
|
||||||
caches.match(cacheKey)
|
// Try the static (client) file cache first
|
||||||
.then((response) => {
|
const cachedResponse = await caches.match(cacheKey);
|
||||||
// Return the cached response if found
|
// Return the cached response if found
|
||||||
if (response) {
|
if (cachedResponse) {
|
||||||
return response;
|
return cachedResponse;
|
||||||
}
|
}
|
||||||
|
|
||||||
const requestUrl = new URL(event.request.url);
|
const requestUrl = new URL(event.request.url);
|
||||||
|
|
||||||
const pathname = requestUrl.pathname;
|
const pathname = requestUrl.pathname;
|
||||||
// console.log("In service worker, pathname is", pathname);
|
// console.log("In service worker, pathname is", pathname);
|
||||||
// Are we fetching a URL from the same origin as the app? If not, we don't handle it here
|
// Are we fetching a URL from the same origin as the app? If not, we don't handle it here
|
||||||
const fetchingLocal = location.host === requestUrl.host;
|
const fetchingLocal = location.host === requestUrl.host;
|
||||||
// If this is a /.fs request, this can either be a plug worker load or an attachment load
|
|
||||||
if (fetchingLocal && pathname.startsWith("/.fs")) {
|
if (!fetchingLocal) {
|
||||||
if (fileContentTable && !event.request.headers.has("x-sync-mode")) {
|
return fetch(event.request);
|
||||||
// console.log(
|
}
|
||||||
// "Attempting to serve file from locally synced space:",
|
|
||||||
// pathname,
|
// If this is a /.fs request, this can either be a plug worker load or an attachment load
|
||||||
// );
|
if (pathname.startsWith("/.fs")) {
|
||||||
// Don't fetch from DB when in sync mode (because then updates won't sync)
|
if (!fileContentTable || event.request.headers.has("x-sync-mode")) {
|
||||||
const path = decodeURIComponent(
|
// Not initialzed yet, or explicitly in sync mode (so direct server communication requested)
|
||||||
requestUrl.pathname.slice("/.fs/".length),
|
|
||||||
);
|
|
||||||
return fileContentTable.get(path).then(
|
|
||||||
async (data) => {
|
|
||||||
if (data) {
|
|
||||||
// console.log("Serving from space", path);
|
|
||||||
if (!data.meta) {
|
|
||||||
// Legacy database not fully synced yet
|
|
||||||
data.meta = (await fileMetatable!.get(path))!;
|
|
||||||
}
|
|
||||||
return new Response(
|
|
||||||
data.data,
|
|
||||||
{
|
|
||||||
headers: {
|
|
||||||
"Content-type": data.meta.contentType,
|
|
||||||
"Content-Length": "" + data.meta.size,
|
|
||||||
"X-Permission": data.meta.perm,
|
|
||||||
"X-Last-Modified": "" + data.meta.lastModified,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
console.error(
|
|
||||||
"Did not find file in locally synced space",
|
|
||||||
path,
|
|
||||||
);
|
|
||||||
return new Response("Not found", {
|
|
||||||
status: 404,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
},
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
// Just fetch the file directly
|
|
||||||
return fetch(event.request);
|
|
||||||
}
|
|
||||||
} else if (fetchingLocal && pathname !== "/.auth") {
|
|
||||||
// Must be a page URL, let's serve index.html which will handle it
|
|
||||||
return caches.match(precacheFiles["/"]).then((response) => {
|
|
||||||
// This shouldnt't happen, index.html not in the cache for some reason
|
|
||||||
return response || fetch(event.request);
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
return fetch(event.request);
|
return fetch(event.request);
|
||||||
}
|
}
|
||||||
}),
|
// console.log(
|
||||||
|
// "Attempting to serve file from locally synced space:",
|
||||||
|
// pathname,
|
||||||
|
// );
|
||||||
|
const path = decodeURIComponent(
|
||||||
|
requestUrl.pathname.slice("/.fs/".length),
|
||||||
|
);
|
||||||
|
const data = await fileContentTable.get(path);
|
||||||
|
if (data) {
|
||||||
|
// console.log("Serving from space", path);
|
||||||
|
if (!data.meta) {
|
||||||
|
// Legacy database not fully synced yet
|
||||||
|
data.meta = (await fileMetatable!.get(path))!;
|
||||||
|
}
|
||||||
|
return new Response(
|
||||||
|
data.data,
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
"Content-type": data.meta.contentType,
|
||||||
|
"Content-Length": "" + data.meta.size,
|
||||||
|
"X-Permission": data.meta.perm,
|
||||||
|
"X-Last-Modified": "" + data.meta.lastModified,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
console.error(
|
||||||
|
"Did not find file in locally synced space",
|
||||||
|
path,
|
||||||
|
);
|
||||||
|
return new Response("Not found", {
|
||||||
|
status: 404,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} else if (pathname === "/.auth") {
|
||||||
|
return fetch(event.request);
|
||||||
|
} else {
|
||||||
|
// Must be a page URL, let's serve index.html which will handle it
|
||||||
|
return (await caches.match(precacheFiles["/"])) || fetch(event.request);
|
||||||
|
}
|
||||||
|
})(),
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue