-
-
Notifications
You must be signed in to change notification settings - Fork 262
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3459419
commit cac24bd
Showing
6 changed files
with
167 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
Source: https://github.com/coonsta/cache-polyfill | ||
Author: https://github.com/coonsta | ||
*/ | ||
|
||
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; | ||
|
||
// Since DOMExceptions are not constructable: | ||
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(); | ||
|
||
// Simulate sequence<(Request or USVString)> binding: | ||
var sequence = []; | ||
|
||
requests = requests.map(function(request) { | ||
if (request instanceof Request) { | ||
return request; | ||
} | ||
else { | ||
return String(request); // may throw TypeError | ||
} | ||
}); | ||
|
||
return Promise.all( | ||
requests.map(function(request) { | ||
if (typeof request === 'string') { | ||
request = new Request(request); | ||
} | ||
|
||
var scheme = new URL(request.url).protocol; | ||
|
||
if (scheme !== 'http:' && scheme !== 'https:') { | ||
throw new NetworkError("Invalid scheme"); | ||
} | ||
|
||
return fetch(request.clone()); | ||
}) | ||
); | ||
}).then(function(responses) { | ||
// TODO: check that requests don't overwrite one another | ||
// (don't think this is possible to polyfill due to opaque responses) | ||
return Promise.all( | ||
responses.map(function(response, i) { | ||
return cache.put(requests[i], response); | ||
}) | ||
); | ||
}).then(function() { | ||
return undefined; | ||
}); | ||
}; | ||
} | ||
|
||
if (!CacheStorage.prototype.match) { | ||
// This is probably vulnerable to race conditions (removing caches etc) | ||
CacheStorage.prototype.match = function match(request, opts) { | ||
var caches = this; | ||
|
||
return this.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()); | ||
}); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
body { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta name="theme-color" content="#2196F3"> | ||
<title>Progressive Web App</title> | ||
<link rel="stylesheet" href="/css/styles.css"> | ||
</head> | ||
<body> | ||
|
||
<h1>Progressive Web Application</h1> | ||
|
||
<script type="text/javascript"> | ||
//If serviceWorker supports, then register it. | ||
if ('serviceWorker' in navigator) { | ||
navigator.serviceWorker | ||
.register('./serviceWorker.js') //Point to serviceWorkerwoker file | ||
.then(function () { | ||
console.log('serviceWorker is registered'); | ||
}); | ||
} | ||
</script> | ||
<script src="js/jquery-2.1.4.js"></script> | ||
<script src="js/app.js"></script> | ||
</body> | ||
</html> |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
|
||
//Cache polyfil to support cacheAPI in all browsers | ||
importScripts('/cache-polyfill.js'); | ||
|
||
var cacheV1 = 'simpleApp'; //Cache name | ||
|
||
//Files to cache | ||
var files = [ | ||
'/', | ||
'/index.html', | ||
'/index.html?page=1', //Query string is treated as new page in serviceWorker | ||
'/css/styles.css', | ||
'/js/app.js', | ||
'/js/jquery-2.1.4.js' | ||
]; | ||
|
||
//Adding install event listener | ||
self.addEventListener('install', function (event) { | ||
event.waitUntil( | ||
caches.open(cacheV1) //To return cached requests | ||
.then(function (cache) { | ||
console.log('Cached'); | ||
//[] of files to cache | ||
return cache.addAll(files); //If any of the file not present compelete `addAll` will fail | ||
}) | ||
); | ||
}); | ||
|
||
/* | ||
FETCH EVENT: will be triggered for every request made by index page | ||
*/ | ||
|
||
//Adding fetch event listener | ||
self.addEventListener('fetch', function (event) { | ||
console.log(event.request.url); //Network request made by the application | ||
|
||
//To tell browser to evaluate the result of event | ||
event.respondWith( | ||
caches.match(event.request) //To match current request with cached request, return it | ||
.then(function (response) { | ||
// Return the response if file is found | ||
// else make fetch request again; | ||
return response || fetch(event.request); | ||
}) | ||
); | ||
}); |