Install & Compatibility
Where this runs
No compatibility data collected yet for this library.
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
CordovaFileCache
✓ const CordovaFileCache = window.CordovaFileCache;
The library exposes a global CordovaFileCache object; no import statement is used. Include the script tag or bundle via <script src='CordovaFileCache.js'></script>.
CordovaPromiseFS
✓ const CordovaPromiseFS = window.CordovaPromiseFS;
Required dependency, also a global. Must be loaded before CordovaFileCache.
Cache instantiation
✓ var cache = new CordovaFileCache({ fs: new CordovaPromiseFS({ Promise: Promise }), mode: 'hash' });
✗ var cache = new CordovaFileCache({ mode: 'hash' }); // missing fs
The CordovaPromiseFS instance is mandatory and must include a Promise implementation.
Complete setup and usage example: initialize cache, add a file, download, and retrieve cached URL.
<!-- Include Cordova, plugins, and promise library first -->
<script src="cordova.js"></script>
<script src="bluebird.js"></script>
<script src="CordovaPromiseFS.js"></script>
<script src="CordovaFileCache.js"></script>
<script>
// Initialize cache after device ready
document.addEventListener('deviceready', function() {
var cache = new CordovaFileCache({
fs: new CordovaPromiseFS({
Promise: Promise // using native Promise or bluebird
}),
mode: 'hash',
localRoot: 'cache',
serverRoot: 'https://example.com/files/',
cacheBuster: true
});
cache.ready.then(function(cachedList) {
console.log('Cache ready, cached paths:', cachedList);
// Add files
cache.add('photo1.jpg'); // prepends serverRoot
cache.download().then(function(cache) {
var internalUrl = cache.get('photo1.jpg');
// internalUrl like 'cdvfile://localhost/persistent/cache/xxx.jpg'
console.log('Cached file URL:', internalUrl);
});
});
});
</script>
Errors
Common errors & fixes
TypeError: Cannot read property 'root' of undefined
CordovaPromiseFS instance not provided or not initialized properly (missing directory).
fixEnsure fs is a valid CordovaPromiseFS instance and that cordova-plugin-file is installed.
CordovaPromiseFS is not defined
CordovaPromiseFS.js script not loaded before CordovaFileCache.js.
fixInclude CordovaPromiseFS.js before CordovaFileCache.js.
TypeError: cache.download is not a function
Cache object not properly initialized or mode is invalid.
fixCheck constructor options: ensure fs and mode are correct.
Audit
Dependencies
cordova-promise-fsrequiredRequired for file system operations (CordovaPromiseFS instance)
cordova-plugin-filerequiredRequired Cordova plugin for file access