Service worker cleanup
parent
f39ab26cea
commit
98bebf3cb8
|
@ -74,12 +74,12 @@ 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(
|
||||||
|
(async () => {
|
||||||
// Try the static (client) file cache first
|
// Try the static (client) file cache first
|
||||||
caches.match(cacheKey)
|
const cachedResponse = await caches.match(cacheKey);
|
||||||
.then((response) => {
|
|
||||||
// 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);
|
||||||
|
@ -88,19 +88,25 @@ self.addEventListener("fetch", (event: any) => {
|
||||||
// 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 (!fetchingLocal) {
|
||||||
|
return fetch(event.request);
|
||||||
|
}
|
||||||
|
|
||||||
// If this is a /.fs request, this can either be a plug worker load or an attachment load
|
// If this is a /.fs request, this can either be a plug worker load or an attachment load
|
||||||
if (fetchingLocal && pathname.startsWith("/.fs")) {
|
if (pathname.startsWith("/.fs")) {
|
||||||
if (fileContentTable && !event.request.headers.has("x-sync-mode")) {
|
if (!fileContentTable || event.request.headers.has("x-sync-mode")) {
|
||||||
|
// Not initialzed yet, or explicitly in sync mode (so direct server communication requested)
|
||||||
|
return fetch(event.request);
|
||||||
|
}
|
||||||
// console.log(
|
// console.log(
|
||||||
// "Attempting to serve file from locally synced space:",
|
// "Attempting to serve file from locally synced space:",
|
||||||
// pathname,
|
// pathname,
|
||||||
// );
|
// );
|
||||||
// Don't fetch from DB when in sync mode (because then updates won't sync)
|
|
||||||
const path = decodeURIComponent(
|
const path = decodeURIComponent(
|
||||||
requestUrl.pathname.slice("/.fs/".length),
|
requestUrl.pathname.slice("/.fs/".length),
|
||||||
);
|
);
|
||||||
return fileContentTable.get(path).then(
|
const data = await fileContentTable.get(path);
|
||||||
async (data) => {
|
|
||||||
if (data) {
|
if (data) {
|
||||||
// console.log("Serving from space", path);
|
// console.log("Serving from space", path);
|
||||||
if (!data.meta) {
|
if (!data.meta) {
|
||||||
|
@ -127,22 +133,13 @@ self.addEventListener("fetch", (event: any) => {
|
||||||
status: 404,
|
status: 404,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
},
|
} else if (pathname === "/.auth") {
|
||||||
);
|
|
||||||
} else {
|
|
||||||
// Just fetch the file directly
|
|
||||||
return fetch(event.request);
|
return fetch(event.request);
|
||||||
}
|
} else {
|
||||||
} else if (fetchingLocal && pathname !== "/.auth") {
|
|
||||||
// Must be a page URL, let's serve index.html which will handle it
|
// Must be a page URL, let's serve index.html which will handle it
|
||||||
return caches.match(precacheFiles["/"]).then((response) => {
|
return (await caches.match(precacheFiles["/"])) || fetch(event.request);
|
||||||
// This shouldnt't happen, index.html not in the cache for some reason
|
|
||||||
return response || fetch(event.request);
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
return fetch(event.request);
|
|
||||||
}
|
}
|
||||||
}),
|
})(),
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue