Skip to content

Instantly share code, notes, and snippets.

@PhilJ
Created March 31, 2016 10:48
Show Gist options
  • Save PhilJ/8f15285b8e12d434e1191d12226ab7b4 to your computer and use it in GitHub Desktop.
Save PhilJ/8f15285b8e12d434e1191d12226ab7b4 to your computer and use it in GitHub Desktop.
Helper to write files to browser cache
// all urls will be added to cache
function cacheAssets( assets ) {
return new Promise( function (resolve, reject) {
// open cache
caches.open('assets')
.then(cache => {
// the API does all the magic for us
cache.addAll(assets)
.then(() => {
console.log('all assets added to cache')
resolve()
})
.catch(err => {
console.log('error when syncing assets', err)
reject()
})
}).catch(err => {
console.log('error when opening cache', err)
reject()
})
});
}
var assets = []; // list of urls to be cached
// cache responses of provided urls
cacheAssets(assets)
.then(() => {
console.log('All assets cached')
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment