inkyboo/sw.js

94 lines
2.8 KiB
JavaScript
Raw Normal View History

2022-10-18 08:41:41 +00:00
const VERSION = '20221018';
2020-12-01 10:49:26 +00:00
const PRECACHE = 'precache-' + VERSION;
const MODES = 'modes-' + VERSION;
// A regexp to detect mode files
2025-02-19 02:14:22 +00:00
const MODE_REGEXP = /^js\/mode\//;
2020-08-21 14:38:42 +00:00
// A list of local resources we always want to be cached.
const PRECACHE_URLS = [
'/',
'script.js',
'style.css',
2020-12-01 10:49:26 +00:00
'favicon.ico',
2025-02-19 02:14:22 +00:00
'js/lzma_worker.min.js',
'js/lzma.min.js',
'js/slimselect.min.js',
'js/clipboard.min.js',
'js/micromodal.min.js',
'js/codemirror.min.js',
'js/loadmode.min.js',
'js/overlay.min.js',
'js/multiplex.min.js',
'js/simple.min.js',
'js/simplescrollbars.js',
'js/meta.min.js',
'css/bootstrap-grid.min.css',
'css/slimselect.min.css',
'css/codemirror.min.css',
'css/simplescrollbars.css',
'css/dracula.min.css',
'css/microtip.min.css',
'fonts/JetBrainsMono-Regular.woff2',
'fonts/roboto-v20.woff2',
2020-08-21 14:38:42 +00:00
];
// The install handler takes care of precaching the resources we always need.
self.addEventListener('install', (event) => {
event.waitUntil(
caches
.open(PRECACHE)
.then((cache) => cache.addAll(PRECACHE_URLS))
.then(self.skipWaiting())
);
});
// The activate handler takes care of cleaning up old caches.
self.addEventListener('activate', (event) => {
2020-12-01 10:49:26 +00:00
const currentCaches = [PRECACHE, MODES];
2020-08-21 14:38:42 +00:00
event.waitUntil(
caches
.keys()
.then((cacheNames) => {
return cacheNames.filter((cacheName) => !currentCaches.includes(cacheName));
})
.then((cachesToDelete) => {
return Promise.all(
cachesToDelete.map((cacheToDelete) => {
return caches.delete(cacheToDelete);
})
);
})
.then(() => self.clients.claim())
);
});
2020-12-01 10:49:26 +00:00
// The fetch handler serves responses from a cache.
2020-08-21 14:38:42 +00:00
// If no response is found, it populates the runtime cache with the response
// from the network before returning it to the page.
self.addEventListener('fetch', (event) => {
2025-02-19 02:14:22 +00:00
if (!event.request.url.startsWith(self.location.origin)) {
2020-08-21 14:38:42 +00:00
return;
}
event.respondWith(
2020-08-21 14:45:08 +00:00
caches.match(event.request, { ignoreSearch: true }).then((cachedResponse) => {
2020-08-21 14:38:42 +00:00
if (cachedResponse) {
return cachedResponse;
}
2020-12-01 10:49:26 +00:00
if (!event.request.url.match(MODE_REGEXP)) {
return fetch(event.request);
}
return caches.open(MODES).then((cache) => {
2020-08-21 14:38:42 +00:00
return fetch(event.request).then((response) => {
// Put a copy of the response in the runtime cache.
return cache.put(event.request, response.clone()).then(() => {
return response;
});
});
});
})
);
});