Skip to content
This repository has been archived by the owner on Sep 18, 2021. It is now read-only.

Update addAll() to reflect new behavior #19

Merged
merged 2 commits into from
Apr 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,25 @@
*
*/

if (!Cache.prototype.addAll) {
(function() {
var nativeAddAll = Cache.prototype.addAll;
var userAgent = navigator.userAgent.match(/(Firefox|Chrome)\/(\d+\.)/);

// Has nice behavior of `var` which everyone hates
if (userAgent) {
var agent = userAgent[1];
var version = parseInt(userAgent[2]);
}

if (
nativeAddAll && (!userAgent ||
(agent === 'Firefox' && version >= 46) ||
(agent === 'Chrome' && version >= 50)
)
) {
return;
}

Cache.prototype.addAll = function addAll(requests) {
var cache = this;

Expand All @@ -25,6 +43,7 @@ if (!Cache.prototype.addAll) {
this.code = 19;
this.message = message;
}

NetworkError.prototype = Object.create(Error.prototype);

return Promise.resolve().then(function() {
Expand Down Expand Up @@ -58,6 +77,14 @@ if (!Cache.prototype.addAll) {
})
);
}).then(function(responses) {
// If some of the responses has not OK-eish status,
// then whole operation should reject
if (responses.some(function(response) {
return !response.ok;
})) {
throw new NetworkError('Incorrect response status');
}

// TODO: check that requests don't overwrite one another
// (don't think this is possible to polyfill due to opaque responses)
return Promise.all(
Expand All @@ -69,4 +96,12 @@ if (!Cache.prototype.addAll) {
return undefined;
});
};
}

// Has native addAll(), but it has to be fixed anyway.
// So add() has to be fixed too
if (nativeAddAll) {
Cache.prototype.add = function add(request) {
return this.addAll([request]);
};
}
}());
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "serviceworker-cache-polyfill",
"version": "3.0.0",
"version": "4.0.0",
"description": "Cache polyfill for the ServiceWorker",
"main": "index.js",
"scripts": {
Expand Down