javascript - Random Service Worker Response Error -
i'm using service worker , cache api cache static resources randomly http request rest api endpoint (which not being cached in sw) fails , message have xhr.statustext
text service worker response error
, response code 500.
i can't tell if error happens urls not being cached or not. there not enough evidence either. happens in chrome (50.0.2661.75 - 64b) , works in firefox 45
i wasn't able reproduce manually happens in selenium tests , appears random. happens on localhost (where sw should work despite plain http) in domain https has self-signed certificate , such sw should not work there ...
selenium tests refreshing pages , closing browser window have no idea if matters.
any ideas why happening or how more information?
update: service worker code:
var version = "sdlkhdfsdfu89q3473lja"; var cache_name = "cache" + version; var cache_pattern = /\.(js|html|css|png|gif|woff|ico)\?v=\s+?$/; function fetchedfromnetwork(response, event) { var cachecopy = response.clone(); var url = event.request.url; if (url.indexof("/api/") === -1 // must not rest api call && url.indexof(version) > -1 // versioned requests && version !== "$cache_version" && cache_pattern.test(url)) { // caches.open(cache_name) .then(function add(cache) { cache.put(event.request, cachecopy); }); } return response; } function unabletoresolve() { return new response("service unavailable", { status: 503, statustext: "service unavailable", headers: new headers({ "content-type": "text/plain" }) }); } this.addeventlistener("fetch", function (event) { // cache if (event.request.method !== "get") { return; } event.respondwith( caches.match(event.request) .then(function (cached) { if (cached) { return cached; } else { return fetch(event.request) .then(function (response) { return fetchedfromnetwork(response, event); }, unabletoresolve) .catch(unabletoresolve); } }, function () { // in case caches.match throws error, fetch request network , rather don't cache time return fetch(event.request); })); }); this.addeventlistener("activate", function (event) { event.waituntil( caches.keys() .then(function (keys) { return promise.all( keys.filter(function (key) { // filter out caches not matching current versioned name return !key.startswith(cache_name); }) .map(function (key) { // remove obsolete caches return caches.delete(key); })); })); });