cordova-file-cache
Super Awesome File Cache for Cordova Apps
Based on cordova-promise-fs.
Getting started
bower install cordova-file-cache cordova-promise-fs
npm install cordova-file-cache cordova-promise-fs
cordova platform add ios@3.7.0
cordova plugin add org.apache.cordova.file
cordova plugin add org.apache.cordova.file-transfer
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
Usage
Initialize & configuration
var cache = CordovaFileCache({
fs: CordovaPromiseFS(),
mode: 'hash',
localRoot: 'data',
serverRoot: 'http://yourserver.com/files/'
cacheBuster: false
});
cache.ready.then(function(list){
})
- mode: "mirror": Mirrors the file structure from
serverRoot
at localRoot
. - mode: "hash": Filename is hash of server url (plus extension).
- CordovaPromiseFS() is an instance of cordova-promise-fs.
cacheBuster
appends a timestamp to the url ?xxxxxx
to avoid the network cache.
Add files to the cache
cache.add('http://yourserver.com/folder/photo1.jpg')
cache.add('folder/photo2.jpg')
cache.add(['photo3.jpg','photo4.jpg'])
cache.isDirty() === true
var dirty = cache.add(['photo3.jpg'])
cache.download(onprogress).then(function(cache){ ... },function(failedDownloads) { ... })
onprogress = function(ProgressEvent) {
ProgressEvent.index
ProgressEvent.total
}
Use the cache
cache.get('photo3.jpg');
cache.toInternalURL('photo3.jpg');
cache.toInternalURL('http://yourserver.com/photo3.jpg');
cache.toURL('photo3.jpg');
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'
cache.toDataURL('photo3.jpg').then(function(base64){},function(err){});
Other functions
cache.abort()
cache.clear().then( ... )
cache.remove('photo3.jpg').then( ... )
cache.toPath('photo3.jpg');
cache.toServerURL('photo3.jpg');
cache.isDirty();
cache.getDownloadQueue();
cache.list().then(function(list){...},function(err){...})
Changelog
0.7.0 (27/11/2014)
- Added tests and fixed few minor bugs
0.6.0 (19/11/2014)
- Bugfix: changes to "get" and "toInternalURL" methods.
- Bugfix: LocalRoot should NOT start with a slash (Android)
0.5.0 (15/11/2014)
- Bugfix: Make sure cache returns a valid server URL if file is not cached.
0.4.0 (13/11/2014)
0.3.0 (09/11/2014)
- Added
cacheBuster
option.
0.2.0 (07/11/2014)
- Many small bugfixes
- Upgraded the build process with
webpack
0.1.0 (06/11/2014)
Contribute
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.
Contact
© 2014 - Mark Marijnissen