Added cachebusting to SW
This commit is contained in:
parent
8ee68a8ff8
commit
9654f7f6bc
@ -4,10 +4,22 @@ self.addEventListener('fetch', function(event) {
|
|||||||
event.respondWith(fetchAndCache(event));
|
event.respondWith(fetchAndCache(event));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function cacheBust(request) {
|
||||||
|
var url = request.url;
|
||||||
|
if (url.indexOf(self.location.origin) >= 0) {
|
||||||
|
if (url.indexOf('.') < 0) {
|
||||||
|
url += `/`;
|
||||||
|
}
|
||||||
|
return `${url}?${Math.random()}`;
|
||||||
|
} else {
|
||||||
|
return request;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function fetchAndCache(event) {
|
function fetchAndCache(event) {
|
||||||
return caches.open(CACHE).then(function (cache) {
|
return caches.open(CACHE).then(function (cache) {
|
||||||
return cache.match(event.request).then(response => {
|
return cache.match(event.request).then(response => {
|
||||||
var fetchResponse = fetch(event.request)
|
var fetchResponse = fetch(cacheBust(event.request))
|
||||||
.then(function(networkResponse) {
|
.then(function(networkResponse) {
|
||||||
cache.put(event.request, networkResponse.clone());
|
cache.put(event.request, networkResponse.clone());
|
||||||
return networkResponse;
|
return networkResponse;
|
||||||
|
Loading…
Reference in New Issue
Block a user