Baseflow / flutter_cached_network_image

Download, cache and show images in a flutter app
https://baseflow.com
2.45k stars 657 forks source link

caching of 404 responses #965

Open qeepcologne opened 3 months ago

qeepcologne commented 3 months ago

🚀 Feature Requests

we fetching image like that which works pretty fine, with the exception of 404 response. Then the image is requested again and again. Don't know why they are not cached (status code or empty response?). Headers should be correct (The images never change (when content change, the uri will change, so we set the immutable header).

CircleAvatar(
    foregroundImage: userImage(_partner!),
    backgroundImage: defaultImage(_partner!))                     

userImage(User partner) => (partner.pictureUrlDisplay != null)
    // there is no configuration, image caching depends on cache control header send by the server
    ? CachedNetworkImageProvider(
        partner.pictureUrlDisplay!,
        headers: userImageHeaders,
        errorListener: (err) {
          _logger.w("failed to load image: $err");
        },
      )
    : defaultImage(partner);

defaultImage(User partner) =>
    AssetImage((partner.gender == "man") ? "assets/image_not_found_man.webp" : "assets/image_not_found_woman.webp");

Describe the feature

allow caching of 404 image requests depending on cache headers. Server already returns suitable header Cache-Control: immutable, max-age=86400 If needed add config similar to CacheOptions in DioCacheInterceptor hitCacheOnErrorExcept: [403, 503, 521], // do not cache temporary errors

RabeaBarabea commented 1 month ago

why the the CachedNetworkImage widget throws an error , when the given URL is invalid , even error handler exists , this causes a test failed in widget testing

qeepcologne commented 1 month ago

maybe this is the reason: https://github.com/Baseflow/flutter_cache_manager/blob/develop/flutter_cache_manager/lib/src/cache_manager.dart (underlying cache manager removes it when status code is 404).