Skip to content

Instantly share code, notes, and snippets.

@darkwing
Created October 22, 2015 20:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save darkwing/b96352480c10fcc64695 to your computer and use it in GitHub Desktop.
Save darkwing/b96352480c10fcc64695 to your computer and use it in GitHub Desktop.
This is the service worker
var EXTRA_FILES = [
"/xjs/_/js/k=xjs.ntp.en_US._WU8vYndmIc.O/m=jsa,ntp,d,csi/rt=j/d=1/t=zcms/rs=ACT90oH3VDY16AdrgBkBZukM4ZIP3OM-xQ",
];
var CHECKSUM = "e0n4rd";
var BLACKLIST = [
'/gen_204\?',
'/async/',
];
var FILES = [
'/images/srpr/logo11w.png',
'/images/srpr/chrome_ntp_white_logo2.png',
'/' + '/ssl.gstatic.com/chrome/components/doodle-notifier-01.html'
].concat(EXTRA_FILES || []);
var CACHENAME = 'newtab-static-' + CHECKSUM;
self.addEventListener('install', function(event) {
event.waitUntil(caches.open(CACHENAME).then(function(cache) {
return cache.addAll(FILES);
}));
});
self.addEventListener('activate', function(event) {
return event.waitUntil(caches.keys().then(function(keys) {
return Promise.all(keys.map(function(k) {
if (k != CACHENAME && k.indexOf('newtab-static-') == 0) {
return caches.delete(k);
} else {
return Promise.resolve();
}
}));
}));
});
self.addEventListener('fetch', function(event) {
event.respondWith(
caches.match(event.request).then(function(response) {
if (response) {
return response;
}
return fetch(event.request).then(function(response) {
var shouldCache = true;
for (var i = 0; i < BLACKLIST.length; ++i) {
var b = new RegExp(BLACKLIST[i]);
if (b.test(event.request.url)) {
shouldCache = false;
break;
}
}
if (event.request.method == 'POST') {
shouldCache = false;
}
if (shouldCache) {
return caches.open(CACHENAME).then(function(cache) {
cache.put(event.request, response.clone());
return response;
});
} else {
return response;
}
});
})
);
});
if (!Cache.prototype.add) {
Cache.prototype.add = function add(request) {
return this.addAll([request]);
};
}
if (!Cache.prototype.addAll) {
Cache.prototype.addAll = function addAll(requests) {
var cache = this;
function NetworkError(message) {
this.name = 'NetworkError';
this.code = 19;
this.message = message;
}
NetworkError.prototype = Object.create(Error.prototype);
return Promise.resolve().then(function() {
if (arguments.length < 1) throw new TypeError();
var sequence = [];
requests = requests.map(function(request) {
if (request instanceof Request) {
return request;
}
else {
return String(request); }
});
return Promise.all(
requests.map(function(request) {
if (typeof request === 'string') {
request = new Request(request);
}
return fetch(request.clone());
})
);
}).then(function(responses) {
return Promise.all(
responses.map(function(response, i) {
return cache.put(requests[i], response);
})
);
}).then(function() {
return undefined;
});
};
}
if (!CacheStorage.prototype.match) {
CacheStorage.prototype.match = function match(request, opts) {
var caches = this;
return caches.keys().then(function(cacheNames) {
var match;
return cacheNames.reduce(function(chain, cacheName) {
return chain.then(function() {
return match || caches.open(cacheName).then(function(cache) {
return cache.match(request, opts);
}).then(function(response) {
match = response;
return match;
});
});
}, Promise.resolve());
});
};
}
@visatronics
Copy link

Charles here, I'm new developer and i was going over my google developer tool settings for the first time and multiple service worker keeps popping up... I don't how to stop them from popping up? Any suggestions?

@deepakkj
Copy link

@visatronics
Open chrome://serviceworker-internals and unselect "Open DevTools window and pause JavaScript execution on Service Worker startup for debugging."

Hope this solves your problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment