sw.js
1 const cacheName = "meloniemacreturn-v6"; 2 const coreAssets = [ 3 4 "/return/", 5 "/return/index.html", 6 "/return/manifest.json", 7 8 "/return/android-chrome-192x192.png", 9 "/return/android-chrome-512x512.png", 10 "/return/apple-touch-icon-57x57-precomposed.png", 11 "/return/apple-touch-icon-57x57.png", 12 "/return/apple-touch-icon-60x60-precomposed.png", 13 "/return/apple-touch-icon-60x60.png", 14 "/return/apple-touch-icon-72x72-precomposed.png", 15 "/return/apple-touch-icon-72x72.png", 16 "/return/apple-touch-icon-76x76-precomposed.png", 17 "/return/apple-touch-icon-76x76.png", 18 "/return/apple-touch-icon-114x114-precomposed.png", 19 "/return/apple-touch-icon-114x114.png", 20 "/return/apple-touch-icon-120x120-precomposed.png", 21 "/return/apple-touch-icon-120x120.png", 22 "/return/apple-touch-icon-144x144-precomposed.png", 23 "/return/apple-touch-icon-144x144.png", 24 "/return/apple-touch-icon-152x152-precomposed.png", 25 "/return/apple-touch-icon-152x152.png", 26 "/return/apple-touch-icon-180x180-precomposed.png", 27 "/return/apple-touch-icon-180x180.png", 28 "/return/apple-touch-icon-precomposed.png", 29 "/return/apple-touch-icon.png", 30 "/return/browserconfig.xml", 31 "/return/favicon-16x16.png", 32 "/return/favicon-32x32.png", 33 "/return/favicon.ico", 34 "/return/icon.png", 35 "/return/icon.svg", 36 "/return/mstile-150x150.png", 37 "/return/safari-pinned-tab.svg", 38 39 "/return/jorge-fernandez-salas-bUz7tSY7kQs-unsplash.jpg" 40 ]; 41 42 43 // On install, cache core assets 44 self.addEventListener('install', function (event) { 45 46 // Cache core assets 47 event.waitUntil(caches.open(cacheName).then(function (cache) { 48 for (let asset of coreAssets) { 49 cache.add(new Request(asset)); 50 } 51 return cache; 52 })); 53 54 }); 55 56 // Listen for request events 57 self.addEventListener('fetch', function (event) { 58 59 // Get the request 60 let request = event.request; 61 62 // Bug fix 63 // https://stackoverflow.com/a/49719964 64 if (event.request.cache === 'only-if-cached' && event.request.mode !== 'same-origin') return; 65 66 // HTML files 67 // Network-first 68 if (request.headers.get('Accept').includes('text/html')) { 69 event.respondWith( 70 fetch(request).then(function (response) { 71 72 // Create a copy of the response and save it to the cache 73 let copy = response.clone(); 74 event.waitUntil(caches.open(cacheName).then(function (cache) { 75 return cache.put(request, copy); 76 })); 77 78 // Return the response 79 return response; 80 81 }).catch(function (error) { 82 83 // If there's no item in cache, respond with a fallback 84 return caches.match(request).then(function (response) { 85 return response || caches.match('/index.html'); 86 }); 87 88 }) 89 ); 90 } 91 92 // CSS & JavaScript 93 // Offline-first 94 if (request.headers.get('Accept').includes('text/css') || request.headers.get('Accept').includes('text/javascript')) { 95 event.respondWith( 96 caches.match(request).then(function (response) { 97 return response || fetch(request).then(function (response) { 98 99 // Return the response 100 return response; 101 102 }); 103 }) 104 ); 105 return; 106 } 107 108 // Images 109 // Offline-first 110 if (request.headers.get('Accept').includes('image')) { 111 event.respondWith( 112 caches.match(request).then(function (response) { 113 return response || fetch(request).then(function (response) { 114 115 // Save a copy of it in cache 116 let copy = response.clone(); 117 event.waitUntil(caches.open(cacheName).then(function (cache) { 118 return cache.put(request, copy); 119 })); 120 121 // Return the response 122 return response; 123 124 }); 125 }) 126 ); 127 } 128 129 });