Super Awesome File Cache for Cordova Apps
Based on cordova-promise-fs.
# fetch code using bower
bower install cordova-file-cache cordova-promise-fs
# ...or npm...
npm install cordova-file-cache cordova-promise-fs
# install Cordova and plugins
cordova platform add ios
cordova plugin add cordova-plugin-file
cordova plugin add cordova-plugin-file-transfer # optional
IMPORTANT: For iOS, use Cordova 3.7.0 or higher (due to a bug that affects requestFileSystem).
Or just download and include CordovaPromiseFS.js and CordovaFileCache.js
// Initialize a Cache
var cache = new CordovaFileCache({
fs: new CordovaPromiseFS({ // An instance of CordovaPromiseFS is REQUIRED
Promise: Promise // <-- your favorite Promise lib (REQUIRED)
}),
mode: 'hash', // or 'mirror', optional
localRoot: 'data', //optional
serverRoot: 'http://yourserver.com/files/', // optional, required on 'mirror' mode
cacheBuster: false // optional
});
cache.ready.then(function(list){
// Promise when cache is ready.
// Returns a list of paths on the FileSystem that are cached.
})
serverRoot
at localRoot
.cacheBuster
appends a timestamp to the url ?xxxxxx
to avoid the network cache.
// First, add files
cache.add('http://yourserver.com/folder/photo1.jpg')
cache.add('folder/photo2.jpg') // automatically prepends the `severRoot`
cache.add(['photo3.jpg','photo4.jpg'])
// Now the cache is dirty: It needs to download.
cache.isDirty() === true
// cache.add also returns if the cache is dirty.
var dirty = cache.add(['photo3.jpg'])
// Downloading files.
// The optional 'onprogress' event handler is enhanced with information
// about the total download queue.
// It is recommended to avoid heavy UI and animation while downloading.
//
// The optional 'includeFileProgress' defaults to "false".
// When set to "true", you will also receive progress events from individual file tranfers.
//
// "false" is recommended as sending many FileTransfer progress events from native to JS can
// slow down performance.
var onprogress = function(e) {
var progress ="Progress: "
+ e.queueIndex // current download index
+ " "
+ e.queueSize; // total files to download
// Download files.
cache.download(onprogress,includeFileProgress).then(function(cache){ ... },function(failedDownloads) { ... })
}
// Get the cached internalURL of the file: "cdvfile://localhost/persisent/cache/photo3.jpg"
cache.get('photo3.jpg');
cache.toInternalURL('photo3.jpg');
cache.toInternalURL('http://yourserver.com/photo3.jpg');
// Get the file URL of the file: "file://.../photo3.jpg";
cache.toURL('photo3.jpg');
// When file is not cached, the server URL is returned as a fallback.
cache.get('http://yoursever.com/never-cached-this.jpg') === 'http://yoursever.com/never-cached-this.jpg'
cache.get('never-cached-this.jpg') === 'http://yoursever.com/never-cached-this.jpg'
// Get Base64 encoded data URL.
cache.toDataURL('photo3.jpg').then(function(base64){},function(err){});
// Abort all downloads
cache.abort()
// Clear cache (removes localRoot directory)
cache.clear().then( ... )
// Or remove a single file
cache.remove('photo3.jpg').then( ... )
// Returns path on Cordova Filesystem, i.e. "/cache/photo3.jpg"
cache.toPath('photo3.jpg');
// Returns server URL to download, i.e. "http://yourserver.com/photo3.jpg";
cache.toServerURL('photo3.jpg');
// Needs a download?
cache.isDirty();
// Returns a list of server URLs that need to be downloaded.
cache.getDownloadQueue();
// Return a list of paths that are cached (i.e. ["/cache/photo3.jpg"])
cache.list().then(function(list){...},function(err){...})
cacheBuster
option.webpack
Convert CommonJS to a browser-version:
npm install webpack -g
npm run-script prepublish
Feel free to contribute to this project in any way. The easiest way to support this project is by giving it a star.
© 2014 - Mark Marijnissen