1
0
Fork 0
mirror of https://github.com/marcrobledo/RomPatcher.js.git synced 2025-06-27 16:25:54 +00:00

PWA fix attempt 4

This commit is contained in:
Marc Robledo 2019-04-18 12:02:38 +02:00
parent 1dd2f6b49f
commit 2bed40b48d
2 changed files with 93 additions and 36 deletions

View file

@ -1,42 +1,99 @@
/* /*
original: https://github.com/GoogleChrome/samples/blob/gh-pages/service-worker/basic/service-worker.js Cache Service Worker template by mrc 2019
mostly based in:
Copyright 2016 Google Inc. All Rights Reserved. https://github.com/GoogleChrome/samples/blob/gh-pages/service-worker/basic/service-worker.js
Licensed under the Apache License, Version 2.0 (the "License"); https://github.com/chriscoyier/Simple-Offline-Site/blob/master/js/service-worker.js
you may not use this file except in compliance with the License. https://gist.github.com/kosamari/7c5d1e8449b2fbc97d372675f16b566e
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0 Note for GitHub Pages:
Unless required by applicable law or agreed to in writing, software there can be an unexpected behaviour (cache not updating) when site is accessed from
distributed under the License is distributed on an "AS IS" BASIS, https://user.github.io/repo/ (without index.html) in some browsers (Firefox)
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. use absolute paths if hosted in GitHub Pages in order to avoid it
See the License for the specific language governing permissions and also invoke sw with an absolute path:
limitations under the License. navigator.serviceWorker.register('/repo/_cache_service_worker.js', {scope: '/repo/'})
*/ */
const PRECACHE = 'precache-v7'; /* MOD: fix old caches for mrc */
const RUNTIME = 'runtime'; caches.keys().then(function(cacheNames){
const PRECACHE_URLS = [ for(var i=0; i<cacheNames.length; i++){
'index.html','/RomPatcher.js/', if(
'manifest.json', cacheNames[i]==='runtime' ||
'favicon.png', /^precache-\w+$/.test(cacheNames[i]) ||
'logo192.png', /^precache-editor-([\w\+]+)-\w+$/.test(cacheNames[i]) ||
'RomPatcher.css', /^v?\d+\w?$/.test(cacheNames[i])
'RomPatcher.js', ){
'locale.js', console.log('deleting old cache: '+cacheNames[i]);
'worker_apply.js', caches.delete(cacheNames[i]);
'worker_create.js', }
'worker_crc.js', }
'MarcFile.js', });
'crc.js',
'ips.js', var PRECACHE_ID='rom-patcher-js';
'ups.js', var PRECACHE_VERSION='v1';
'aps.js', var PRECACHE_URLS=[
'bps.js', '/RomPatcher.js/','/RomPatcher.js/index.html',
'rup.js', '/RomPatcher.js/manifest.json',
'ppf.js', '/RomPatcher.js/favicon.png',
'vcdiff.js' '/RomPatcher.js/logo192.png',
'/RomPatcher.js/RomPatcher.css',
'/RomPatcher.js/RomPatcher.js',
'/RomPatcher.js/locale.js',
'/RomPatcher.js/worker_apply.js',
'/RomPatcher.js/worker_create.js',
'/RomPatcher.js/worker_crc.js',
'/RomPatcher.js/MarcFile.js',
'/RomPatcher.js/crc.js',
'/RomPatcher.js/ips.js',
'/RomPatcher.js/ups.js',
'/RomPatcher.js/aps.js',
'/RomPatcher.js/bps.js',
'/RomPatcher.js/rup.js',
'/RomPatcher.js/ppf.js',
'/RomPatcher.js/vcdiff.js'
]; ];
self.addEventListener('install', event => {event.waitUntil(caches.open(PRECACHE).then(cache => cache.addAll(PRECACHE_URLS)).then(self.skipWaiting()));});self.addEventListener('activate', event => {const currentCaches = [PRECACHE, RUNTIME];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()));});self.addEventListener('fetch', event => {if (event.request.url.startsWith(self.location.origin)) {event.respondWith(caches.match(event.request).then(cachedResponse => {if (cachedResponse) {return cachedResponse;}return caches.open(RUNTIME).then(cache => {return fetch(event.request).then(response => {return cache.put(event.request, response.clone()).then(() => {return response;});});});}));}});
//PRECACHE_ID='precache-'+PRECACHE_ID+'-'+PRECACHE_VERSION;
// 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 => {
return cacheNames.filter(cacheName => (cacheName.startsWith('precache-'+PRECACHE_ID+'-') && !cacheName.endsWith('-'+PRECACHE_VERSION)));
}).then(cachesToDelete => {
return 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) {
console.log('retrieving cached resource: '+cachedResource;
return cachedResource;
}else{
console.log('FAILED retrieving cached resource: '+evt.request;
return fetch(evt.request);
}
})
);
}
});

View file

@ -122,7 +122,7 @@
<!-- FOOTER --> <!-- FOOTER -->
<footer> <footer>
Rom Patcher JS <small>v2.0 RC1b</small> by <a href="/">Marc Robledo</a> Rom Patcher JS <small>v2.0 RC1c</small> by <a href="/">Marc Robledo</a>
<br /> <br />
<i class="icon github"></i> <a href="https://github.com/marcrobledo/RomPatcher.js/" target="_blank">See on GitHub</a> <i class="icon github"></i> <a href="https://github.com/marcrobledo/RomPatcher.js/" target="_blank">See on GitHub</a>
<i class="icon heart"></i> <a href="https://www.paypal.me/marcrobledo/5" target="_blank" rel="nofollow">Donate</a> <i class="icon heart"></i> <a href="https://www.paypal.me/marcrobledo/5" target="_blank" rel="nofollow">Donate</a>