From 2bed40b48db0fa9221ac0a0d005e1c28805f7df3 Mon Sep 17 00:00:00 2001 From: Marc Robledo Date: Thu, 18 Apr 2019 12:02:38 +0200 Subject: [PATCH] PWA fix attempt 4 --- _cache_service_worker.js | 127 ++++++++++++++++++++++++++++----------- index.html | 2 +- 2 files changed, 93 insertions(+), 36 deletions(-) diff --git a/_cache_service_worker.js b/_cache_service_worker.js index d18da3e..3c5cc5e 100644 --- a/_cache_service_worker.js +++ b/_cache_service_worker.js @@ -1,42 +1,99 @@ /* -original: https://github.com/GoogleChrome/samples/blob/gh-pages/service-worker/basic/service-worker.js - -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. + 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/'}) */ -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 {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;});});});}));}}); \ No newline at end of file + +//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); + } + }) + ); + } +}); \ No newline at end of file diff --git a/index.html b/index.html index b64884d..d2fc6ff 100644 --- a/index.html +++ b/index.html @@ -122,7 +122,7 @@