Last active
February 11, 2016 12:08
-
-
Save gokulkrishh/3fa1e10fd1fbb628013e to your computer and use it in GitHub Desktop.
Service worker installation steps
This file contains hidden or 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
//Cache polyfil to support cacheAPI in browsers | |
importScripts('/cache-polyfill.js'); | |
//Cache name | |
var staticCache = "my-static-files"; | |
//Files to cache | |
var filesToCache = [ | |
"/", | |
"images/logo.jpg", | |
"css/main.css", | |
"js/app.js" | |
]; | |
//Adding a eventlistener to install event | |
self.addEventListener("install", function (event) { | |
//Installation steps | |
event.waitUntil( | |
caches.open(staticCache) | |
.then(function (cache) { | |
//[] of files to cache & if any of the file not present compelete `addAll` will fail | |
return cache.addAll(urlsToCache) | |
.then(function () { | |
console.log("Successfully cached."); | |
}) | |
.cache(function (error) { | |
console.log("Failed to cache ", error); | |
}) | |
}) | |
); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment