add shitty WIP fix for preventing-cache
This commit is contained in:
parent
16034014a0
commit
fe0fc12a8a
2 changed files with 72 additions and 60 deletions
12
index.html
12
index.html
|
|
@ -638,5 +638,17 @@
|
||||||
</footer>
|
</footer>
|
||||||
</div>
|
</div>
|
||||||
<script type="module" src="js/app.js"></script>
|
<script type="module" src="js/app.js"></script>
|
||||||
|
<script>
|
||||||
|
if ('serviceWorker' in navigator) {
|
||||||
|
navigator.serviceWorker.register('/sw.js').then(reg => {
|
||||||
|
reg.update();
|
||||||
|
});
|
||||||
|
|
||||||
|
navigator.serviceWorker.addEventListener('controllerchange', () => {
|
||||||
|
location.reload();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
||||||
118
sw.js
118
sw.js
|
|
@ -1,66 +1,66 @@
|
||||||
//sw.js
|
// sw.js
|
||||||
const CACHE_NAME = 'monochrome-v4';
|
const SW_VERSION = 'monochrome-v4'; // Note To Self: Change Every Deploy
|
||||||
const urlsToCache = [
|
const CACHE_NAME = `monochrome-${SW_VERSION}`;
|
||||||
'/',
|
|
||||||
'/index.html',
|
const ASSETS = [
|
||||||
'/styles.css',
|
'/',
|
||||||
'/js/app.js',
|
'/index.html',
|
||||||
'/js/api.js',
|
'/styles.css',
|
||||||
'/js/player.js',
|
|
||||||
'/js/storage.js',
|
'/js/app.js',
|
||||||
'/js/ui.js',
|
'/js/api.js',
|
||||||
'/js/utils.js',
|
'/js/player.js',
|
||||||
'/js/cache.js',
|
'/js/storage.js',
|
||||||
'/js/router.js',
|
'/js/ui.js',
|
||||||
'/js/events.js',
|
'/js/utils.js',
|
||||||
'/js/ui-interactions.js',
|
'/js/cache.js',
|
||||||
'/js/settings.js',
|
'/js/router.js',
|
||||||
'/js/lastfm.js',
|
'/js/events.js',
|
||||||
'/js/lyrics.js',
|
'/js/ui-interactions.js',
|
||||||
'/js/downloads.js',
|
'/js/settings.js',
|
||||||
'/js/db.js',
|
'/js/lastfm.js',
|
||||||
'/js/metadata.js',
|
'/js/lyrics.js',
|
||||||
'/manifest.json',
|
'/js/downloads.js',
|
||||||
'/assets/logo.svg',
|
'/js/db.js',
|
||||||
'/assets/appicon.png',
|
'/js/metadata.js',
|
||||||
'/assets/96.png'
|
|
||||||
|
'/manifest.json',
|
||||||
|
'/assets/logo.svg',
|
||||||
|
'/assets/appicon.png',
|
||||||
|
'/assets/96.png'
|
||||||
];
|
];
|
||||||
|
|
||||||
self.addEventListener('install', event => {
|
self.addEventListener('install', event => {
|
||||||
self.skipWaiting();
|
self.skipWaiting();
|
||||||
event.waitUntil(
|
event.waitUntil(
|
||||||
caches.open(CACHE_NAME)
|
caches.open(CACHE_NAME).then(cache => cache.addAll(ASSETS))
|
||||||
.then(cache => cache.addAll(urlsToCache))
|
);
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
self.addEventListener('fetch', event => {
|
|
||||||
const url = new URL(event.request.url);
|
|
||||||
|
|
||||||
// Static Assets & App Shell (Cache-First)
|
|
||||||
event.respondWith(
|
|
||||||
caches.match(event.request)
|
|
||||||
.then(response => {
|
|
||||||
// Return cached response if found
|
|
||||||
if (response) return response;
|
|
||||||
|
|
||||||
// Otherwise fetch from network
|
|
||||||
return fetch(event.request);
|
|
||||||
})
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
self.addEventListener('activate', event => {
|
self.addEventListener('activate', event => {
|
||||||
const cacheWhitelist = [CACHE_NAME];
|
event.waitUntil(
|
||||||
event.waitUntil(
|
caches.keys().then(keys =>
|
||||||
caches.keys().then(cacheNames => {
|
Promise.all(
|
||||||
return Promise.all(
|
keys
|
||||||
cacheNames.map(cacheName => {
|
.filter(key => key !== CACHE_NAME)
|
||||||
if (cacheWhitelist.indexOf(cacheName) === -1) {
|
.map(key => caches.delete(key))
|
||||||
return caches.delete(cacheName);
|
)
|
||||||
}
|
)
|
||||||
})
|
);
|
||||||
).then(() => self.clients.claim());
|
self.clients.claim();
|
||||||
})
|
});
|
||||||
);
|
|
||||||
|
self.addEventListener('fetch', event => {
|
||||||
|
const req = event.request;
|
||||||
|
|
||||||
|
if (req.mode === 'navigate') {
|
||||||
|
event.respondWith(fetch(req));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
event.respondWith(
|
||||||
|
caches.match(req).then(cached =>
|
||||||
|
cached || fetch(req)
|
||||||
|
)
|
||||||
|
);
|
||||||
});
|
});
|
||||||
Loading…
Reference in a new issue