Created
February 25, 2016 03:11
-
-
Save mkhatib/1d812451fc1dcd1ac051 to your computer and use it in GitHub Desktop.
Service Worker with a problem of same hash with two different cache content on two different browsers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
importScripts("workers/sw-toolbox-workers.js"); | |
var PrecacheConfig = [ | |
["404.html", "85f3907148cdb68254687c3de7f89a25"], | |
["bower_components/angular-file-upload/dist/angular-file-upload-shim.min.js", "026ef609992ad0f8d1b02dae399a764b"], | |
["favicon.ico", "b2eb3d352ea4e8f74b160b46ccc881b9"], | |
["https://ajax.googleapis.com/ajax/libs/angularjs/1.3.17/angular.min.js", "cd13a2e1aad01b347ad15d5e201f39ee"], | |
["https://fonts.googleapis.com/earlyaccess/droidarabicnaskh.css", "0d94281129940b9c1a522545882c8293"], | |
["https://fonts.gstatic.com/ea/droidarabicnaskh/v7/DroidNaskh-Bold.ttf", "aedd83523a65f2c0dc4e2bad69b94534"], | |
["https://fonts.gstatic.com/ea/droidarabicnaskh/v7/DroidNaskh-Bold.woff", "78aa34ad512567cf0b5e7fc611ff3099"], | |
["https://fonts.gstatic.com/ea/droidarabicnaskh/v7/DroidNaskh-Bold.woff2", "259e057bdcb62221cd168102311c9d71"], | |
["https://fonts.gstatic.com/ea/droidarabicnaskh/v7/DroidNaskh-Regular.ttf", "fff65a06d7c535c7345c90f3d3feb5bb"], | |
["https://fonts.gstatic.com/ea/droidarabicnaskh/v7/DroidNaskh-Regular.woff", "0c04f7df2ef66d3ec56022ca6cbfcc68"], | |
["https://fonts.gstatic.com/ea/droidarabicnaskh/v7/DroidNaskh-Regular.woff2", "00ffb798cafe0d339a68c643d85b1cd8"], | |
["https://yandex.st/highlightjs/8.0/highlight.min.js", "84693e3fcef453f832de7a753d4e0a78"], | |
["https://yandex.st/highlightjs/8.0/styles/default.min.css", "0b218e6d7104ecc6444db91163872965"], | |
["images/[email protected]", "3309e9f60e1f0fd10df41dc0e42cc8fb"], | |
["images/[email protected]", "2ea8f0dc90b8fd60198f8959f5ce119d"], | |
["images/[email protected]", "256e48dd9d5e81bb72ec640f1a2f8caa"], | |
["images/[email protected]", "67c3bf674e35d8f51d0dd1bf938b129b"], | |
["images/[email protected]", "7a5eef8ab1cb111379ccc9e364ec56cc"], | |
["images/[email protected]", "e128549736610d86d1d810b73b171b72"], | |
["images/[email protected]", "62e4160be41ed9bbaaff92045369ca83"], | |
["images/[email protected]", "c88ef24b4b3bf6afb9094f1e56a9752b"], | |
["images/[email protected]", "7f424d3cfd739bfb77940abd01a6225e"], | |
["images/[email protected]", "29b3d347b8529a1df74a32ebe63c75a6"], | |
["index.html", "2cdd5371d1201f857054a716570c1564"], | |
["scripts/8913e40c.modules.js", "f72913833c2b2ed46aa30858c746e9a2"], | |
["styles/9ca9353e.enhanced.css", "9ca9353efb7c18f648d0bdfb41a24d51"], | |
["styles/9ee667bb.main.css", "5101c11a60b249a0e61ec8ea24893cc6"], | |
["styles/fonts/01c9dd9c.fa-manshar-webfont.woff2", "84d5db1d1e8b64e49e282a1a777e170f"], | |
["styles/fonts/323ceb68.fa-manshar-webfont.svg", "323ceb6891c2c08d65a48ce1174db7b5"], | |
["styles/fonts/6f1855a5.fa-manshar-webfont.ttf", "82704e52d8d41933a28278e919d05942"], | |
["styles/fonts/d8d98f00.fa-manshar-webfont.eot", "5718eec0104ae80ae7fb0fc91d96b3bd"], | |
["styles/fonts/e49251ab.fa-manshar-webfont.woff", "21782020b0b945ae05a61375d76ec637"] | |
]; | |
var CacheNamePrefix = 'sw-precache-v1-webclient-' + (self.registration ? self.registration.scope : '') + '-'; | |
var IgnoreUrlParametersMatching = [/^utm_/]; | |
var addDirectoryIndex = function(originalUrl, index) { | |
var url = new URL(originalUrl); | |
if (url.pathname.slice(-1) === '/') { | |
url.pathname += index; | |
} | |
return url.toString(); | |
}; | |
var populateCurrentCacheNames = function(precacheConfig, cacheNamePrefix, baseUrl) { | |
var absoluteUrlToCacheName = {}; | |
var currentCacheNamesToAbsoluteUrl = {}; | |
precacheConfig.forEach(function(cacheOption) { | |
var absoluteUrl = new URL(cacheOption[0], baseUrl).toString(); | |
var cacheName = cacheNamePrefix + absoluteUrl + '-' + cacheOption[1]; | |
currentCacheNamesToAbsoluteUrl[cacheName] = absoluteUrl; | |
absoluteUrlToCacheName[absoluteUrl] = cacheName; | |
}); | |
return { | |
absoluteUrlToCacheName: absoluteUrlToCacheName, | |
currentCacheNamesToAbsoluteUrl: currentCacheNamesToAbsoluteUrl | |
}; | |
}; | |
var stripIgnoredUrlParameters = function(originalUrl, ignoreUrlParametersMatching) { | |
var url = new URL(originalUrl); | |
url.search = url.search.slice(1).split('&').map(function(kv) { | |
return kv.split('='); | |
}).filter(function(kv) { | |
return ignoreUrlParametersMatching.every(function(ignoredRegex) { | |
return !ignoredRegex.test(kv[0]); | |
}); | |
}).map(function(kv) { | |
return kv.join('='); | |
}).join('&'); | |
return url.toString(); | |
}; | |
var mappings = populateCurrentCacheNames(PrecacheConfig, CacheNamePrefix, self.location); | |
var AbsoluteUrlToCacheName = mappings.absoluteUrlToCacheName; | |
var CurrentCacheNamesToAbsoluteUrl = mappings.currentCacheNamesToAbsoluteUrl; | |
function deleteAllCaches() { | |
return caches.keys().then(function(cacheNames) { | |
return Promise.all(cacheNames.map(function(cacheName) { | |
return caches.delete(cacheName); | |
})); | |
}); | |
} | |
self.addEventListener('install', function(event) { | |
var now = Date.now(); | |
event.waitUntil(caches.keys().then(function(allCacheNames) { | |
return Promise.all(Object.keys(CurrentCacheNamesToAbsoluteUrl).filter(function(cacheName) { | |
return allCacheNames.indexOf(cacheName) === -1; | |
}).map(function(cacheName) { | |
var url = new URL(CurrentCacheNamesToAbsoluteUrl[cacheName]); | |
if (url.search) { | |
url.search += '&'; | |
} | |
url.search += 'sw-precache=' + now; | |
var urlWithCacheBusting = url.toString(); | |
console.log('Adding URL "%s" to cache named "%s"', urlWithCacheBusting, cacheName); | |
return caches.open(cacheName).then(function(cache) { | |
var request = new Request(urlWithCacheBusting, { | |
credentials: 'same-origin' | |
}); | |
return fetch(request.clone()).then(function(response) { | |
if (response.ok) { | |
return cache.put(request, response); | |
} | |
console.error('Request for %s returned a response with status %d, so not attempting to cache it.', urlWithCacheBusting, response.status); | |
return caches.delete(cacheName); | |
}); | |
}); | |
})).then(function() { | |
return Promise.all(allCacheNames.filter(function(cacheName) { | |
return cacheName.indexOf(CacheNamePrefix) === 0 && !(cacheName in CurrentCacheNamesToAbsoluteUrl); | |
}).map(function(cacheName) { | |
console.log('Deleting out-of-date cache "%s"', cacheName); | |
return caches.delete(cacheName); | |
})); | |
}); | |
}).then(function() { | |
if (typeof self.skipWaiting === 'function') { | |
self.skipWaiting(); | |
} | |
})); | |
}); | |
if (self.clients && (typeof self.clients.claim === 'function')) { | |
self.addEventListener('activate', function(event) { | |
event.waitUntil(self.clients.claim()); | |
}); | |
} | |
self.addEventListener('message', function(event) { | |
if (event.data.command === 'delete_all') { | |
console.log('About to delete all caches...'); | |
deleteAllCaches().then(function() { | |
console.log('Caches deleted.'); | |
event.ports[0].postMessage({ | |
error: null | |
}); | |
}).catch(function(error) { | |
console.log('Caches not deleted:', error); | |
event.ports[0].postMessage({ | |
error: error | |
}); | |
}); | |
} | |
}); | |
self.addEventListener('fetch', function(event) { | |
if (event.request.method === 'GET') { | |
var urlWithoutIgnoredParameters = stripIgnoredUrlParameters(event.request.url, IgnoreUrlParametersMatching); | |
var cacheName = AbsoluteUrlToCacheName[urlWithoutIgnoredParameters]; | |
var directoryIndex = 'index.html'; | |
if (!cacheName && directoryIndex) { | |
urlWithoutIgnoredParameters = addDirectoryIndex(urlWithoutIgnoredParameters, directoryIndex); | |
cacheName = AbsoluteUrlToCacheName[urlWithoutIgnoredParameters]; | |
} | |
var navigateFallback = ''; | |
if (!cacheName && navigateFallback && event.request.headers.has('accept') && event.request.headers.get('accept').includes('text/html')) { | |
var navigateFallbackUrl = new URL(navigateFallback, self.location); | |
cacheName = AbsoluteUrlToCacheName[navigateFallbackUrl.toString()]; | |
} | |
if (cacheName) { | |
event.respondWith(caches.open(cacheName).then(function(cache) { | |
return cache.keys().then(function(keys) { | |
return cache.match(keys[0]).then(function(response) { | |
return response || fetch(event.request).catch(function(e) { | |
console.error('Fetch for "%s" failed: %O', urlWithoutIgnoredParameters, e); | |
}); | |
}); | |
}); | |
}).catch(function(e) { | |
console.error('Couldn\'t serve response for "%s" from cache: %O', urlWithoutIgnoredParameters, e); | |
return fetch(event.request); | |
})); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment