mirror of
https://github.com/marcrobledo/savegame-editors.git
synced 2025-10-06 00:12:46 +02:00
* Add icons to all items * Add leashes * Add bowls * Renamed skins to interiors * Cleanup formatting
83 lines
3.0 KiB
JavaScript
83 lines
3.0 KiB
JavaScript
/*
|
|
Cache Service Worker template by mrc 2019
|
|
mostly based in:
|
|
https://github.com/GoogleChrome/samples/blob/gh-pages/service-worker/basic/service-worker.js
|
|
https://github.com/chriscoyier/Simple-Offline-Site/blob/master/js/service-worker.js
|
|
https://gist.github.com/kosamari/7c5d1e8449b2fbc97d372675f16b566e
|
|
|
|
Note for GitHub Pages:
|
|
there can be an unexpected behaviour (cache not updating) when site is accessed from
|
|
https://user.github.io/repo/ (without index.html) in some browsers (Firefox)
|
|
use absolute paths if hosted in GitHub Pages in order to avoid it
|
|
also invoke sw with an absolute path:
|
|
navigator.serviceWorker.register('/repo/_cache_service_worker.js', {scope: '/repo/'})
|
|
*/
|
|
|
|
/* MOD: fix old caches for mrc */
|
|
caches.keys().then( ( cacheNames ) => {
|
|
for ( let i = 0; i < cacheNames.length; i++ ) {
|
|
if (
|
|
cacheNames[ i ] === 'runtime' ||
|
|
/^precache-\w+$/.test( cacheNames[ i ] ) ||
|
|
/^precache-editor-([\w\+]+)-\w+$/.test( cacheNames[ i ] ) ||
|
|
/^v?\d+\w?$/.test( cacheNames[ i ] )
|
|
) {
|
|
console.log( 'deleting old cache: ' + cacheNames[ i ] );
|
|
caches.delete( cacheNames[ i ] );
|
|
}
|
|
}
|
|
} );
|
|
|
|
const PRECACHE_ID = 'nintendogs+cats-editor';
|
|
const PRECACHE_VERSION = 'v1';
|
|
const PRECACHE_URLS = [
|
|
'/savegame-editors/nintendogs+cats/',
|
|
'/savegame-editors/nintendogs+cats/index.html',
|
|
'/savegame-editors/nintendogs+cats/favicon.png',
|
|
'/savegame-editors/nintendogs+cats/nintendogs+cats.js',
|
|
'/savegame-editors/nintendogs+cats/items.js',
|
|
'/savegame-editors/nintendogs+cats/personalities.js',
|
|
'/savegame-editors/nintendogs+cats/dialog.js',
|
|
'/savegame-editors/nintendogs+cats/dialog.css',
|
|
'/savegame-editors/savegame-editor.js',
|
|
'/savegame-editors/savegame-editor.css'
|
|
];
|
|
|
|
// install event (fired when sw is first installed): opens a new cache
|
|
self.addEventListener( 'install', ( evt ) => {
|
|
evt.waitUntil(
|
|
caches.open( 'precache-' + PRECACHE_ID + '-' + PRECACHE_VERSION )
|
|
.then( ( cache ) => cache.addAll( PRECACHE_URLS ) )
|
|
.then( self.skipWaiting() )
|
|
);
|
|
} );
|
|
|
|
// activate event (fired when sw is has been successfully installed): cleans up old outdated caches
|
|
self.addEventListener( 'activate', ( evt ) => {
|
|
evt.waitUntil(
|
|
caches.keys().then(
|
|
( cacheNames ) => cacheNames.filter( ( cacheName ) => ( cacheName.startsWith( 'precache-' + PRECACHE_ID + '-' ) && !cacheName.endsWith( '-' + PRECACHE_VERSION ) ) )
|
|
).then(
|
|
( cachesToDelete ) => Promise.all( cachesToDelete.map( ( cacheToDelete ) => {
|
|
console.log( 'delete ' + cacheToDelete );
|
|
return caches.delete( cacheToDelete );
|
|
} ) )
|
|
).then( () => self.clients.claim() )
|
|
);
|
|
} );
|
|
|
|
// fetch event (fired when requesting a resource): returns cached resource when possible
|
|
self.addEventListener( 'fetch', ( evt ) => {
|
|
if ( evt.request.url.startsWith( self.location.origin ) ) { // skip cross-origin requests
|
|
evt.respondWith(
|
|
caches.match( evt.request ).then( ( cachedResource ) => {
|
|
if ( cachedResource ) {
|
|
return cachedResource;
|
|
} else {
|
|
return fetch( evt.request );
|
|
}
|
|
} )
|
|
);
|
|
}
|
|
} );
|