187 lines
5.6 KiB
JavaScript
187 lines
5.6 KiB
JavaScript
const RUNTIME_CACHE = "map-studio-runtime-v9";
|
|
const DATA_MAX_AGE_MS = 180 * 24 * 60 * 60 * 1000;
|
|
|
|
self.addEventListener("install", () => {
|
|
self.skipWaiting();
|
|
});
|
|
|
|
self.addEventListener("activate", (event) => {
|
|
event.waitUntil(
|
|
Promise.all([
|
|
self.clients.claim(),
|
|
caches.keys().then((keys) =>
|
|
Promise.all(
|
|
keys
|
|
.filter(
|
|
(key) =>
|
|
key.startsWith("map-studio-runtime-") &&
|
|
key !== RUNTIME_CACHE
|
|
)
|
|
.map((key) => caches.delete(key))
|
|
)
|
|
)
|
|
])
|
|
);
|
|
});
|
|
|
|
const isTileRequest = (url) => /\/tiles\//.test(url.pathname);
|
|
const isVendorRequest = (url) => /\/vendor\//.test(url.pathname);
|
|
const isStaticAsset = (url) => {
|
|
const path = url.pathname;
|
|
return (
|
|
path.endsWith("/styles.css") ||
|
|
path.endsWith("/app.js") ||
|
|
path.endsWith("/coordinate-systems.js") ||
|
|
path.endsWith("/compliance-gate.js") ||
|
|
path.endsWith("/print-block.js") ||
|
|
isVendorRequest(url)
|
|
);
|
|
};
|
|
const isDataApi = (url) =>
|
|
url.pathname.endsWith("/api/rail") ||
|
|
url.pathname.includes("/api/rail-tiles/") ||
|
|
url.pathname.endsWith("/api/water") ||
|
|
url.pathname.endsWith("/api/boundaries") ||
|
|
url.pathname.includes("/api/local-vector/") ||
|
|
url.pathname.includes("/api/local-vector-25/");
|
|
const isStatusApi = (url) =>
|
|
url.pathname.endsWith("/api/rail/status") ||
|
|
url.pathname.endsWith("/api/water/status") ||
|
|
url.pathname.endsWith("/api/local-vector/status") ||
|
|
url.pathname.endsWith("/api/local-vector-25/status") ||
|
|
url.pathname.endsWith("/api/status");
|
|
|
|
const responseIsFresh = (response) => {
|
|
const date = Date.parse(response.headers.get("Date") || "");
|
|
return Number.isFinite(date) && Date.now() - date <= DATA_MAX_AGE_MS;
|
|
};
|
|
|
|
const withoutRefresh = (requestUrl) => {
|
|
const normalized = new URL(requestUrl.href);
|
|
normalized.searchParams.delete("refresh");
|
|
return normalized.href;
|
|
};
|
|
|
|
const networkAndCache = async (request, cache) => {
|
|
const response = await fetch(request);
|
|
const placeholder = response.headers.get("X-Cache") === "PLACEHOLDER";
|
|
if (response.status === 200 && !placeholder) {
|
|
try {
|
|
await cache.put(request, response.clone());
|
|
} catch (error) {
|
|
// A quota or cache-storage failure must not fail an otherwise valid response.
|
|
}
|
|
}
|
|
return response;
|
|
};
|
|
|
|
const cacheFirst = async (request, cache, expireData = false) => {
|
|
let cached = await cache.match(request);
|
|
if (cached && cached.headers.get("X-Cache") === "PLACEHOLDER") {
|
|
await cache.delete(request);
|
|
cached = undefined;
|
|
}
|
|
if (cached && cached.ok && (!expireData || responseIsFresh(cached))) {
|
|
return cached;
|
|
}
|
|
try {
|
|
const response = await networkAndCache(request, cache);
|
|
if (response.status === 202 && isDataApi(new URL(request.url))) {
|
|
await cache.delete(withoutRefresh(new URL(request.url)));
|
|
await cache.delete(request);
|
|
}
|
|
return response;
|
|
} catch (error) {
|
|
if (cached) return cached;
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
const cacheFirstStatic = async (request, cache) => {
|
|
const cached = await cache.match(request);
|
|
if (cached && cached.ok && cached.headers.get("Content-Length") !== "0") {
|
|
return cached;
|
|
}
|
|
if (cached) await cache.delete(request);
|
|
|
|
let lastError = null;
|
|
for (let attempt = 0; attempt < 3; attempt += 1) {
|
|
try {
|
|
const response = await fetch(request, { cache: attempt ? "reload" : "default" });
|
|
if (response.ok) {
|
|
const cacheKey = new URL(request.url);
|
|
cacheKey.searchParams.delete("retry");
|
|
try {
|
|
await cache.put(cacheKey.href, response.clone());
|
|
} catch (error) {
|
|
// Serve the network response even if the browser cache is unavailable.
|
|
}
|
|
return response;
|
|
}
|
|
if (response.status === 404) return response;
|
|
lastError = new Error("HTTP " + response.status);
|
|
} catch (error) {
|
|
lastError = error;
|
|
}
|
|
if (attempt < 2) {
|
|
await new Promise((resolve) => setTimeout(resolve, 250 * (attempt + 1)));
|
|
}
|
|
}
|
|
if (cached) return cached;
|
|
throw lastError || new Error("Static asset unavailable");
|
|
};
|
|
|
|
self.addEventListener("fetch", (event) => {
|
|
const request = event.request;
|
|
if (request.method !== "GET") return;
|
|
const url = new URL(request.url);
|
|
if (url.origin !== self.location.origin || isStatusApi(url)) return;
|
|
|
|
if (request.mode === "navigate") {
|
|
event.respondWith(
|
|
(async () => {
|
|
const cache = await caches.open(RUNTIME_CACHE);
|
|
try {
|
|
return await networkAndCache(request, cache);
|
|
} catch (error) {
|
|
return (await cache.match(request)) || (await cache.match(url.pathname)) || Response.error();
|
|
}
|
|
})()
|
|
);
|
|
return;
|
|
}
|
|
|
|
if (isDataApi(url)) {
|
|
const forceRefresh = url.searchParams.get("refresh") === "1";
|
|
event.respondWith(
|
|
(async () => {
|
|
const cache = await caches.open(RUNTIME_CACHE);
|
|
if (forceRefresh) {
|
|
await cache.delete(withoutRefresh(url));
|
|
await cache.delete(request);
|
|
const response = await fetch(request);
|
|
if (response.status === 200) {
|
|
await cache.put(withoutRefresh(url), response.clone());
|
|
await cache.put(request, response.clone());
|
|
}
|
|
return response;
|
|
}
|
|
return cacheFirst(request, cache, true);
|
|
})()
|
|
);
|
|
return;
|
|
}
|
|
|
|
if (isTileRequest(url) || isStaticAsset(url)) {
|
|
event.respondWith(
|
|
(async () => {
|
|
const cache = await caches.open(RUNTIME_CACHE);
|
|
if (isStaticAsset(url)) {
|
|
return cacheFirstStatic(request, cache);
|
|
}
|
|
return cacheFirst(request, cache, true);
|
|
})()
|
|
);
|
|
}
|
|
});
|