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