49 lines
2.1 KiB
JavaScript
49 lines
2.1 KiB
JavaScript
// Bump VERSION whenever a shell file changes. New versions wait for approval.
|
|
const VERSION = 'v9';
|
|
const BASE = self.registration.scope;
|
|
const PREFIX = `ascii-pwa-${encodeURIComponent(BASE)}-`;
|
|
const CACHE = `${PREFIX}${VERSION}`;
|
|
const SHELL = [
|
|
'./', './index.html', './style.css', './app.js',
|
|
'./renderer.js', './font-catalog.js', './vendor/figlet.js', './vendor/alpine.js',
|
|
'./translations.js', './pwa.js', './manifest.webmanifest',
|
|
'./fonts/IBMPlexMono-Regular.subset.woff2',
|
|
'./fonts/IBMPlexMono-Bold.subset.woff2',
|
|
'./icons/ascii.svg', './icons/icon-192.png', './icons/icon-512.png',
|
|
'./icons/apple-touch-icon.png', './icons/maskable.svg',
|
|
'./icons/maskable-192.png', './icons/maskable-512.png',
|
|
].map(path => new URL(path, BASE).href);
|
|
const HOME = new URL('./', BASE).href;
|
|
const ROUTES = new Set([new URL(HOME).pathname, new URL('./index.html', BASE).pathname]);
|
|
|
|
self.addEventListener('install', event => {
|
|
event.waitUntil(caches.open(CACHE).then(cache => cache.addAll(SHELL)));
|
|
});
|
|
self.addEventListener('message', event => {
|
|
if (event.data === 'ACTIVATE_UPDATE') self.skipWaiting();
|
|
});
|
|
self.addEventListener('activate', event => {
|
|
event.waitUntil((async () => {
|
|
const names = await caches.keys();
|
|
await Promise.all(names.filter(name => name.startsWith(PREFIX) && name !== CACHE)
|
|
.map(name => caches.delete(name)));
|
|
await self.clients.claim();
|
|
})());
|
|
});
|
|
self.addEventListener('fetch', event => {
|
|
const {request} = event;
|
|
const url = new URL(request.url);
|
|
if (request.method !== 'GET' || url.origin !== self.location.origin) return;
|
|
if (request.mode === 'navigate') {
|
|
if (!ROUTES.has(url.pathname)) return;
|
|
event.respondWith(caches.open(CACHE).then(async cache =>
|
|
(await cache.match(HOME)) || fetch(request)));
|
|
} else {
|
|
url.search = '';
|
|
url.hash = '';
|
|
if (!SHELL.includes(url.href)) return;
|
|
event.respondWith(caches.open(CACHE).then(async cache =>
|
|
(await cache.match(url.href)) || fetch(request)));
|
|
}
|
|
});
|