PWA support attempt 1 #20

This commit is contained in:
Ian Renton
2025-10-03 17:10:01 +01:00
parent ff4128ef9e
commit c1a649c678
2 changed files with 69 additions and 0 deletions

View File

@@ -0,0 +1,42 @@
const CACHE_NAME = 'Spothole';
const CACHE_URLS = [
'index.html',
'./',
'apidocs',
'apidocs/openapi.yml',
'about',
'css/style.css',
'js/code.js',
'img/logo.png',
'img/favicon.ico',
'img/icon-32.png',
'img/icon-192.png',
'img/icon-512.png',
'fa/css/fontawesome.min.css',
'fa/css/solid.min.css',
'fa/webfonts/fa-solid-900.ttf',
'fa/webfonts/fa-solid-900.woff2'
];
self.addEventListener('fetch', (event) => {
// Is this an asset we can cache?
const url = new URL(event.request.url);
const isCacheableRequest = CACHE_URLS.includes(url.pathname);
if (isCacheableRequest) {
// Open the cache
event.respondWith(caches.open(CACHE_NAME).then((cache) => {
// Go to the network first, cacheing the response
return fetch(event.request.url).then((fetchedResponse) => {
cache.put(event.request, fetchedResponse.clone());
return fetchedResponse;
}).catch(() => {
// If the network is unavailable, get from cache.
return cache.match(event.request.url);
});
}));
} else {
// Not a cacheable request, must be a call to the API, so no cache involved just go to the network
}
});