1(function () { 2 'use strict'; 3 4 // Update 'version' if you need to refresh the cache 5 var version = 'v1.0::CacheFirstSafe'; 6 var offlineUrl = "/offline.html"; 7 8 // Store core files in a cache (including a page to display when offline) 9 function updateStaticCache() { 10 return caches.open(version) 11 .then(function (cache) { 12 return cache.addAll([ 13 offlineUrl, 14 15 ]); 16 }); 17 } 18 19 function addToCache(request, response) { 20 if (!response.ok && response.type !== 'opaque') 21 return; 22 23 var copy = response.clone(); 24 caches.open(version) 25 .then(function (cache) { 26 cache.put(request, copy); 27 }); 28 } 29 30 function serveOfflineImage(request) { 31 if (request.headers.get('Accept').indexOf('image') !== -1) { 32 return new Response('<svg role="img" aria-labelledby="offline-title" viewBox="0 0 400 300" xmlns="http://www.w3.org/2000/svg"><title id="offline-title">Offline</title><g fill="none" fill-rule="evenodd"><path fill="#D8D8D8" d="M0 0h400v300H0z"/><text fill="#9B9B9B" font-family="Helvetica Neue,Arial,Helvetica,sans-serif" font-size="72" font-weight="bold"><tspan x="93" y="172">offline</tspan></text></g></svg>', { headers: { 'Content-Type': 'image/svg+xml' } }); 33 } 34 } 35 36 self.addEventListener('install', function (event) { 37 event.waitUntil(updateStaticCache()); 38 }); 39 40 self.addEventListener('activate', function (event) { 41 event.waitUntil( 42 caches.keys() 43 .then(function (keys) { 44 // Remove caches whose name is no longer valid 45 return Promise.all(keys 46 .filter(function (key) { 47 return key.indexOf(version) !== 0; 48 }) 49 .map(function (key) { 50 return caches.delete(key); 51 }) 52 ); 53 }) 54 ); 55 }); 56 57 self.addEventListener('fetch', function (event) { 58 var request = event.request; 59 60 // Always fetch non-GET requests from the network 61 if (request.method !== 'GET' || request.url.match(/\/browserLink/ig)) { 62 event.respondWith( 63 fetch(request) 64 .catch(function () { 65 return caches.match(offlineUrl); 66 }) 67 ); 68 return; 69 } 70 71 // For HTML requests, try the network first, fall back to the cache, finally the offline page 72 if (request.headers.get('Accept').indexOf('text/html') !== -1) { 73 event.respondWith( 74 fetch(request) 75 .then(function (response) { 76 // Stash a copy of this page in the cache 77 addToCache(request, response); 78 return response; 79 }) 80 .catch(function () { 81 return caches.match(request) 82 .then(function (response) { 83 return response || caches.match(offlineUrl); 84 }); 85 }) 86 ); 87 return; 88 } 89 90 // cache first for fingerprinted resources 91 if (request.url.match(/(\?|&)v=/ig)) { 92 event.respondWith( 93 caches.match(request) 94 .then(function (response) { 95 return response || fetch(request) 96 .then(function (response) { 97 addToCache(request, response); 98 return response || serveOfflineImage(request); 99 }) 100 .catch(function () { 101 return serveOfflineImage(request); 102 }); 103 }) 104 ); 105 106 return; 107 } 108 109 // network first for non-fingerprinted resources 110 event.respondWith( 111 fetch(request) 112 .then(function (response) { 113 // Stash a copy of this page in the cache 114 addToCache(request, response); 115 return response; 116 }) 117 .catch(function () { 118 return caches.match(request) 119 .then(function (response) { 120 return response || serveOfflineImage(request); 121 }) 122 .catch(function () { 123 return serveOfflineImage(request); 124 }); 125 }) 126 ); 127 }); 128 129})();
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.