Created
March 31, 2016 10:48
-
-
Save PhilJ/8f15285b8e12d434e1191d12226ab7b4 to your computer and use it in GitHub Desktop.
Helper to write files to browser cache
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
// 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