Skip to content

Commit 07eb2e7

Browse files
eduardoRothihadeed
authored andcommitted
fix(cache): Images not saved to cache (#171)
* fix(wkwebview): Images not loading in Ionic WKWebView * fix(livereload): Images not loading When running in an emulator or in a device with the flag --livereload images were not showing. * lint(livereload): Updated for ihadeed comments * fix(livereload): Using reject instead of resolve Reject caused an error and did not return the URL. Fixed this resolving and returning the image URL back to the promise. * fix(cache): Images not saved to cache If you didn't set a maxCacheSize, images won't be stored in the cache and will not load in your component. Changed shouldIndex property based in @felixbroehl modifications. * fix(cache): Removed unused check cache space func
1 parent 2022b9c commit 07eb2e7

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

src/providers/image-loader.ts

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ export class ImageLoader {
6565

6666
private indexed: boolean = false;
6767

68-
private get shouldIndex(): boolean {
69-
return (this.config.maxCacheAge > -1) || (this.config.maxCacheSize > -1);
68+
private get isCacheSpaceExceeded(): boolean {
69+
return this.config.maxCacheSize > -1 && this.currentCacheSize > this.config.maxCacheSize;
7070
}
7171

7272
private get isWKWebView(): boolean {
@@ -76,9 +76,9 @@ export class ImageLoader {
7676
private get isIonicWKWebView(): boolean {
7777
return this.isWKWebView && (location.host === 'localhost:8080' || (<any>window).LiveReload);
7878
}
79-
79+
8080
private get isDevServer() : boolean {
81-
return (window['IonicDevServer'] != undefined);
81+
return (window['IonicDevServer'] != undefined);
8282
}
8383

8484
constructor(
@@ -289,16 +289,17 @@ export class ImageLoader {
289289
}).subscribe(
290290
(data: Blob) => {
291291
this.file.writeFile(localDir, fileName, data, {replace: true}).then((file: FileEntry) => {
292-
if (this.shouldIndex) {
293-
this.addFileToIndex(file).then(() => {
294-
this.getCachedImagePath(currentItem.imageUrl).then((localUrl) => {
295-
currentItem.resolve(localUrl);
296-
resolve();
297-
done();
298-
this.maintainCacheSize();
299-
});
300-
});
292+
if (this.isCacheSpaceExceeded) {
293+
this.maintainCacheSize();
301294
}
295+
this.addFileToIndex(file).then(() => {
296+
this.getCachedImagePath(currentItem.imageUrl).then((localUrl) => {
297+
currentItem.resolve(localUrl);
298+
resolve();
299+
done();
300+
this.maintainCacheSize();
301+
});
302+
});
302303
}).catch((e) => {
303304
//Could not write image
304305
error(e);
@@ -383,9 +384,6 @@ export class ImageLoader {
383384
*/
384385
private indexCache(): Promise<void> {
385386

386-
// only index if needed, to save resources
387-
if (!this.shouldIndex) return Promise.resolve();
388-
389387
this.cacheIndex = [];
390388

391389
return this.file.listDir(this.file.cacheDirectory, this.config.cacheDirectoryName)
@@ -468,7 +466,7 @@ export class ImageLoader {
468466
if (!this.isCacheReady) {
469467
return reject();
470468
}
471-
469+
472470
// if we're running with livereload, ignore cache and call the resource from it's URL
473471
if(this.isDevServer){
474472
return resolve(url);

0 commit comments

Comments
 (0)