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:
parent
1dd2f6b49f
commit
2bed40b48d
2 changed files with 93 additions and 36 deletions
|
@ -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:
|
||||
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
|
||||
|
||||
Copyright 2016 Google Inc. All Rights Reserved.
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
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/'})
|
||||
*/
|
||||
|
||||
|
||||
const PRECACHE = 'precache-v7';
|
||||
const RUNTIME = 'runtime';
|
||||
const PRECACHE_URLS = [
|
||||
'index.html','/RomPatcher.js/',
|
||||
'manifest.json',
|
||||
'favicon.png',
|
||||
'logo192.png',
|
||||
'RomPatcher.css',
|
||||
'RomPatcher.js',
|
||||
'locale.js',
|
||||
'worker_apply.js',
|
||||
'worker_create.js',
|
||||
'worker_crc.js',
|
||||
'MarcFile.js',
|
||||
'crc.js',
|
||||
'ips.js',
|
||||
'ups.js',
|
||||
'aps.js',
|
||||
'bps.js',
|
||||
'rup.js',
|
||||
'ppf.js',
|
||||
'vcdiff.js'
|
||||
/* MOD: fix old caches for mrc */
|
||||
caches.keys().then(function(cacheNames){
|
||||
for(var 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]);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
var PRECACHE_ID='rom-patcher-js';
|
||||
var PRECACHE_VERSION='v1';
|
||||
var PRECACHE_URLS=[
|
||||
'/RomPatcher.js/','/RomPatcher.js/index.html',
|
||||
'/RomPatcher.js/manifest.json',
|
||||
'/RomPatcher.js/favicon.png',
|
||||
'/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);
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
});
|
|
@ -122,7 +122,7 @@
|
|||
|
||||
<!-- 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 />
|
||||
<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>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue