154 lines
3.5 KiB
JavaScript
154 lines
3.5 KiB
JavaScript
const CACHE_NAME = 'the-door-v3';
|
|
const NAVIGATION_TIMEOUT_MS = 2500;
|
|
const OFFLINE_FALLBACK_PATH = '/crisis-offline.html';
|
|
const PRECACHE_ASSETS = [
|
|
'/',
|
|
'/index.html',
|
|
'/about.html',
|
|
'/manifest.json',
|
|
'/crisis-offline.html',
|
|
'/testimony.html'
|
|
];
|
|
|
|
function isSameOrigin(request) {
|
|
return new URL(request.url).origin === self.location.origin;
|
|
}
|
|
|
|
function canCache(response) {
|
|
return Boolean(response && response.ok && response.type !== 'opaque');
|
|
}
|
|
|
|
async function precache() {
|
|
const cache = await caches.open(CACHE_NAME);
|
|
await cache.addAll(PRECACHE_ASSETS);
|
|
}
|
|
|
|
async function cleanupOldCaches() {
|
|
const keys = await caches.keys();
|
|
await Promise.all(
|
|
keys
|
|
.filter((key) => key !== CACHE_NAME)
|
|
.map((key) => caches.delete(key))
|
|
);
|
|
}
|
|
|
|
async function putInCache(request, response) {
|
|
if (!isSameOrigin(request) || !canCache(response)) {
|
|
return response;
|
|
}
|
|
|
|
const cache = await caches.open(CACHE_NAME);
|
|
await cache.put(request, response.clone());
|
|
return response;
|
|
}
|
|
|
|
async function fetchWithTimeout(request, timeoutMs) {
|
|
const controller = new AbortController();
|
|
const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
|
|
|
|
try {
|
|
return await fetch(request, { signal: controller.signal });
|
|
} finally {
|
|
clearTimeout(timeoutId);
|
|
}
|
|
}
|
|
|
|
async function offlineTextResponse() {
|
|
return new Response('Offline. Call 988 or text HOME to 741741 for immediate help.', {
|
|
status: 503,
|
|
statusText: 'Service Unavailable',
|
|
headers: new Headers({ 'Content-Type': 'text/plain; charset=utf-8' })
|
|
});
|
|
}
|
|
|
|
async function handleNavigation(request) {
|
|
const cache = await caches.open(CACHE_NAME);
|
|
const cachedPage = await cache.match(request);
|
|
const offlineFallback = await cache.match(OFFLINE_FALLBACK_PATH);
|
|
|
|
try {
|
|
const response = await fetchWithTimeout(request, NAVIGATION_TIMEOUT_MS);
|
|
return await putInCache(request, response);
|
|
} catch (error) {
|
|
if (cachedPage) {
|
|
return cachedPage;
|
|
}
|
|
|
|
if (offlineFallback) {
|
|
return offlineFallback;
|
|
}
|
|
|
|
return offlineTextResponse();
|
|
}
|
|
}
|
|
|
|
async function handleStaticRequest(request) {
|
|
const cache = await caches.open(CACHE_NAME);
|
|
const cached = await cache.match(request);
|
|
|
|
if (cached) {
|
|
fetch(request)
|
|
.then((response) => putInCache(request, response))
|
|
.catch(() => null);
|
|
return cached;
|
|
}
|
|
|
|
try {
|
|
const response = await fetch(request);
|
|
return await putInCache(request, response);
|
|
} catch (error) {
|
|
return offlineTextResponse();
|
|
}
|
|
}
|
|
|
|
async function handleOtherRequest(request) {
|
|
try {
|
|
const response = await fetch(request);
|
|
return await putInCache(request, response);
|
|
} catch (error) {
|
|
const cached = await caches.match(request);
|
|
if (cached) {
|
|
return cached;
|
|
}
|
|
|
|
return offlineTextResponse();
|
|
}
|
|
}
|
|
|
|
self.addEventListener('install', (event) => {
|
|
event.waitUntil(
|
|
precache().then(() => self.skipWaiting())
|
|
);
|
|
});
|
|
|
|
self.addEventListener('activate', (event) => {
|
|
event.waitUntil(
|
|
cleanupOldCaches().then(() => self.clients.claim())
|
|
);
|
|
});
|
|
|
|
self.addEventListener('fetch', (event) => {
|
|
const request = event.request;
|
|
const url = new URL(request.url);
|
|
|
|
if (request.method !== 'GET') {
|
|
return;
|
|
}
|
|
|
|
if (!isSameOrigin(request) || url.pathname.startsWith('/api/')) {
|
|
return;
|
|
}
|
|
|
|
if (event.request.mode === 'navigate') {
|
|
event.respondWith(handleNavigation(request));
|
|
return;
|
|
}
|
|
|
|
if (PRECACHE_ASSETS.includes(url.pathname)) {
|
|
event.respondWith(handleStaticRequest(request));
|
|
return;
|
|
}
|
|
|
|
event.respondWith(handleOtherRequest(request));
|
|
});
|