Files
Heimdall/src/Heimdall.Web/wwwroot/service-worker.js
T
arnaud.dartois 636154572b
Heimdall Gitea / Detect CI scope (push) Successful in 6s
Heimdall Gitea / Build and tests (push) Successful in 19m19s
Heimdall Gitea / Publish versioned release (push) Skipped
Heimdall Gitea / Build and scan server image (push) Successful in 6m13s
Heimdall Gitea / Build and scan agent image (push) Successful in 14m44s
Heimdall Gitea / Publish Docker Hub latest tags (push) Successful in 1m48s
Heimdall Gitea / Build and scan NVIDIA Agent image (push) Successful in 19m9s
Heimdall Gitea / CI policy (push) Successful in 10s
Complete remaining Heimdall features and acceptance
Merge pull request #138

Co-authored-by: Arnaud Dartois <admin@tordais.cc>
2026-11-06 11:14:49 +01:00

70 lines
1.9 KiB
JavaScript

const CACHE_NAME = "heimdall-public-shell-v1";
const OFFLINE_PAGE = "/offline.html";
const PUBLIC_SHELL = [
OFFLINE_PAGE,
"/manifest.webmanifest",
"/pwa-icon.svg"
];
const NO_OFFLINE_FALLBACK_PATHS = [
"/api",
"/_auth",
"/_blazor",
"/hubs",
"/hangfire",
"/metrics",
"/health",
"/healthz",
"/swagger",
"/docs",
"/scalar",
"/openapi"
];
function hasPathPrefix(pathname, prefix) {
let decodedPath;
try {
decodedPath = decodeURIComponent(pathname).toLowerCase();
} catch {
return true;
}
return decodedPath === prefix || decodedPath.startsWith(`${prefix}/`);
}
self.addEventListener("install", event => {
event.waitUntil(
caches.open(CACHE_NAME)
.then(cache => cache.addAll(PUBLIC_SHELL))
);
});
self.addEventListener("message", event => {
if (event.data?.type === "ACTIVATE_UPDATE") {
self.skipWaiting();
}
});
self.addEventListener("activate", event => {
event.waitUntil((async () => {
const names = await caches.keys();
await Promise.all(names
.filter(name => name.startsWith("heimdall-public-shell-") && name !== CACHE_NAME)
.map(name => caches.delete(name)));
await self.clients.claim();
})());
});
self.addEventListener("fetch", event => {
const request = event.request;
if (request.method !== "GET" || request.mode !== "navigate") return;
const requestUrl = new URL(request.url);
if (requestUrl.origin !== self.location.origin) return;
if (NO_OFFLINE_FALLBACK_PATHS.some(prefix => hasPathPrefix(requestUrl.pathname, prefix))) return;
// Navigation responses may contain authenticated, user-specific data. Always
// fetch them from the server and use only the public offline page on failure.
// Operational, API and technical routes retain their normal network behavior.
event.respondWith(fetch(request).catch(async () =>
(await caches.match(OFFLINE_PAGE)) ?? Response.error()));
});