flutter_cached_network_image icon indicating copy to clipboard operation
flutter_cached_network_image copied to clipboard

caching of 404 responses

Open qeepcologne opened this issue 1 year ago • 5 comments

🚀 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

qeepcologne avatar Aug 19 '24 08:08 qeepcologne

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

RabeaBarabea avatar Oct 09 '24 08:10 RabeaBarabea

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).

qeepcologne avatar Oct 10 '24 15:10 qeepcologne

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).

Did you find a workaround for this?

Edit: If you have a fallback url/image you can nest two CachedNetworkImage widgets like:

CachedNetworkImage(
  imageUrl: primaryUrl, // Your main image URL
  placeholder: (context, url) => Center(child: CircularProgressIndicator()),
  errorWidget: (context, url, error) => CachedNetworkImage(
    imageUrl: fallbackUrl, // Fallback image URL
    // Using the original image cache key helps avoid repeated fetch attempts for broken URLs.
    cacheKey: primaryUrl,
    placeholder: (context, url) => Center(child: CircularProgressIndicator()),
    errorWidget: (context, url, error) => Image.asset(
      'assets/placeholder.png', // Local asset fallback
    ),
  ),
);

EdgarMagalhaes avatar Mar 03 '25 11:03 EdgarMagalhaes

On 3/3/25, 12:43 "Edgar Magalhães" @.***> wrote:

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).

Did you find a workaround for this?—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you authored the thread.Message ID: @.***>

EdgarMagalhaes left a comment (Baseflow/flutter_cached_network_image#965)

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).

Did you find a workaround for this?

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you authored the thread.Message ID: @.***>

qeepcologne avatar Mar 04 '25 09:03 qeepcologne

nice idea with the nested CachedNetworkImage, but this won't work for us. The errorWidget is asset not CachedNetworkImage.

class _FallbackImage extends StatelessWidget {
  const _FallbackImage(this.gender, this.fit);

  final String gender;
  final BoxFit fit;

  @override
  Widget build(BuildContext context) =>
      Image.asset((gender == "man") ? "assets/image_not_found_man.webp" : "assets/image_not_found_woman.webp",
          fit: fit);
}

qeepcologne avatar Mar 04 '25 09:03 qeepcologne