javascript - Response whose "body" is locked cannot be used to respond to a request -
i trying service workers in google chrome. stumbled upon error. googling error gives 1 single result @ moment seems in google chrome sourcecode.
i not convinced error bug. when try in firefox, corrupted content error screen. occurs when handling fetch event project root:
self.addeventlistener('fetch', function(event) { // nice url var requesturl = new url(event.request.url); console.log("request for: ", requesturl.href); // network, cache, fallback event.respondwith( // try download fetch(event.request) .then(function(x) { console.log(" "+requesturl.href+" >> ",x); // if failed x not ok if(x && x.ok) { // if result ok save cache cache.put(event.request, x); return x; } else // try fetch cached version if request failed return cache.match(event.request); }) // in case of error return 404 page .catch(function(error) { console.warn(" "+requesturl.href+" >> unavailable: ",error); return cache.match('/page/content-not-available-offline'); }) ); });
i'm not sure body locked error means, don't know code relevant.
the problem body
of response (the actual content, such html or javascript) can used once purposes, such saving cache or using actual response.
to solve this, response.prototype.clone()
method exists.
in fact, main reason
clone()
exists allow multiple uses ofbody
objects (when one-use only.)
in case, problem storing response in cache , returning it. correct approach is:
if(x && x.ok) { // if result ok save cache cache.put(event.request, x.clone()); return x; }
Comments
Post a Comment