Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
gokulkrishh committed Dec 28, 2015
1 parent 3459419 commit cac24bd
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 0 deletions.
88 changes: 88 additions & 0 deletions cache-polyfill.js
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());
});
};
}
3 changes: 3 additions & 0 deletions css/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {

}
26 changes: 26 additions & 0 deletions index.html
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>
Empty file added js/app.js
Empty file.
4 changes: 4 additions & 0 deletions js/jquery-2.1.4.js

Large diffs are not rendered by default.

46 changes: 46 additions & 0 deletions serviceWorker.js
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);
})
);
});

0 comments on commit cac24bd

Please sign in to comment.