Files
Termix/public/sw.js
T

98 lines
2.1 KiB
JavaScript
Raw Normal View History

2026-05-06 15:12:07 -05:00
const CACHE_NAME = "termix-static-v2";
2026-05-28 22:05:25 -04:00
const BASE_PATH = "__TERMIX_SW_BASE_PATH__";
2026-01-24 19:49:42 -06:00
const STATIC_ASSETS = [
2026-05-28 22:05:25 -04:00
`${BASE_PATH}/favicon.ico`,
`${BASE_PATH}/icons/48x48.png`,
`${BASE_PATH}/icons/128x128.png`,
`${BASE_PATH}/icons/256x256.png`,
`${BASE_PATH}/icons/512x512.png`,
2026-01-24 19:49:42 -06:00
];
self.addEventListener("install", (event) => {
event.waitUntil(
caches
.open(CACHE_NAME)
.then((cache) => {
return cache.addAll(STATIC_ASSETS);
})
.then(() => {
return self.skipWaiting();
}),
);
});
self.addEventListener("activate", (event) => {
event.waitUntil(
caches
.keys()
.then((cacheNames) => {
return Promise.all(
cacheNames
.filter((name) => name !== CACHE_NAME)
.map((name) => {
return caches.delete(name);
}),
);
})
.then(() => {
return self.clients.claim();
}),
);
});
self.addEventListener("fetch", (event) => {
const { request } = event;
const url = new URL(request.url);
if (request.method !== "GET") {
return;
}
if (url.pathname.startsWith("/api/") || url.pathname.startsWith("/ws")) {
return;
}
+5
2026-03-08 18:02:14 -05:00
if (
+9
2026-04-22 16:55:23 -05:00
url.pathname.startsWith("/host/opkssh-chooser/") ||
url.pathname.startsWith("/host/opkssh-callback/")
+5
2026-03-08 18:02:14 -05:00
) {
return;
}
2026-01-24 19:49:42 -06:00
if (url.origin !== self.location.origin) {
return;
}
if (request.mode === "navigate") {
2026-05-06 15:12:07 -05:00
event.respondWith(fetch(request));
2026-01-24 19:49:42 -06:00
return;
}
2026-05-06 15:12:07 -05:00
const isStaticAsset = STATIC_ASSETS.some((asset) => url.pathname === asset);
2026-01-26 23:06:34 -06:00
if (!isStaticAsset) {
return;
}
2026-01-24 19:49:42 -06:00
event.respondWith(
caches.match(request).then((cachedResponse) => {
if (cachedResponse) {
return cachedResponse;
}
return fetch(request).then((response) => {
if (!response || response.status !== 200 || response.type !== "basic") {
return response;
}
const responseClone = response.clone();
caches.open(CACHE_NAME).then((cache) => {
cache.put(request, responseClone);
});
return response;
});
}),
);
});