Skip to content

Instantly share code, notes, and snippets.

@gokulkrishh
Last active February 11, 2016 12:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gokulkrishh/3fa1e10fd1fbb628013e to your computer and use it in GitHub Desktop.
Save gokulkrishh/3fa1e10fd1fbb628013e to your computer and use it in GitHub Desktop.
Service worker installation steps
//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