[0.32] Image caching broken
There has been quite a noticeable regression with image caching in 0.31. The symptoms in my app are that any image rendered multiple times is fetched (and re-decoded?) for each Image instance. This is especially apparent with small avatar images, which in the past would have no delay in appearing if the same one was rendered in in a previous Image component.
The symptoms can be observed on iOS; both simulator and device, in both debug and release mode; I have not checked on Android, and have not tested with 0.33 rc. I can create a video to illustrate the problem, but currently traveling to London and won't be able until I return back to the states next week.
I suspect it has to do with the significant image cache refactor that happened on these commits: f12d0a2 54244e1 ddc70ff
I originally mentioned #9431 but here we can track the issue separately.
cc @bestander @mkonicek @javache Would like to cc Maria Mateescu (author on all three commits above) but don't actually know her github handle. Maybe @matemariaescu ?
@sjmueller, any help would be welcome. If you don't mind, please create a video and a way to reproduce it. I'll contact Maria and ask her to have a look
@sjmueller, I looked into the caching and the image cache is being hit. My diffs did not change the implementation, rather separated the cache from the loader. What I suspect may be causing the issue would be the fetchDate being part of the cacheKey. This was added in 631785f, @javache may be able to provide some more context.
@sjmueller: can you provide an example of an image (URL) that fails to be cached?
It would be really nice to understand how image caching works in RN and how one could leverage the nuances of the code. The cache behavior/logic is documented somewhere?
any progress? seems the images are only cached in several seconds, and then need new fetch
React Native for iOS does not implement its own network caching for images. Instead we rely on the system's default NSURLCache (see http://nshipster.com/nsurlcache/ for details). If your images are refetched continuously, this may point to a misconfiguration of the cache headers sent by your server.
@javache, I'm maintaining a react-native photo library component and I've been experiencing cache problem since version 0.25. I tested the problem a lot and I see that's happening when I create jsbundle file with react-native bundler and embed it within the app. Cache is working fine when I serve jsbundle from my computer. Here is the Github issue for the problem: https://github.com/halilb/react-native-photo-browser/issues/8
I've used some Flickr images for this test and I can also confirm http responses have valid cache headers.
@sjmueller Can you check my issue to see if it seems relevant with yours?
I started seeing caching problems when upgrading from 0.30 to 0.32. Looked like images reloaded every time. Reverted back to 0.30
I'm seeing this as well. My packager is echoing the same images over and over (which is new to 0.32 vs 0.31)
[9:35:07 AM] <END> processing asset request shared/images/icons/[email protected] (256ms)
[9:35:07 AM] <START> processing asset request shared/images/icons/[email protected]
[9:35:07 AM] <START> processing asset request shared/images/icons/[email protected]
[9:35:07 AM] <START> processing asset request shared/images/icons/[email protected]
[9:35:07 AM] <START> processing asset request shared/images/icons/[email protected]
[9:35:08 AM] <END> processing asset request shared/images/icons/[email protected] (178ms)
[9:35:08 AM] <END> processing asset request shared/images/icons/[email protected] (178ms)
[9:35:08 AM] <END> processing asset request shared/images/icons/[email protected] (176ms)
[9:35:08 AM] <END> processing asset request shared/images/icons/[email protected] (178ms)
[9:35:08 AM] <START> processing asset request shared/images/icons/[email protected]
[9:35:08 AM] <END> processing asset request shared/images/icons/[email protected] (40ms)
[9:35:08 AM] <START> processing asset request shared/images/icons/[email protected]
[9:35:08 AM] <START> processing asset request shared/images/icons/[email protected]
[9:35:08 AM] <START> processing asset request shared/images/icons/[email protected]
[9:35:08 AM] <START> processing asset request shared/images/icons/[email protected]
[9:35:08 AM] <END> processing asset request shared/images/icons/[email protected] (208ms)
[9:35:08 AM] <END> processing asset request shared/images/icons/[email protected] (207ms)
[9:35:08 AM] <END> processing asset request shared/images/icons/[email protected] (207ms)
[9:35:08 AM] <END> processing asset request shared/images/icons/[email protected] (197ms)
I'm trying to reproduce a crash that happens when I toggle images, unfortunately I was not able to reproduce the crash, but here's basically what I'm trying to do:
https://github.com/rclai/react-native-image-caching-problem
My original problem is that if I toggle the images without the style attribute, it's fine, but once I introduce the style tag, I get a crash that looks like this:
2016-08-31 14:49:33.681 [info][tid:main][RCTImageView.m:354] [PERF IMAGEVIEW] Reloading image http://192.168.1.130:8081/assets/images/play.png?platform=ios&hash=8857a07b95f27926aae603a0d11605ac as size {123, 180.5}
2016-08-31 14:49:33.681 [error][tid:com.facebook.react.JavaScript] Module RCTLog is not a registered callable module.
2016-08-31 14:49:33.684 [fatal][tid:com.facebook.react.RCTExceptionsManagerQueue] Unhandled JS Exception: Module RCTLog is not a registered callable module.
2016-08-31 14:49:33.687 [error][tid:com.facebook.react.JavaScript] Module RCTLog is not a registered callable module.
2016-08-31 14:49:33.690 [fatal][tid:com.facebook.react.RCTExceptionsManagerQueue] Unhandled JS Exception: Module RCTLog is not a registered callable module.
2016-08-31 14:49:33.691 [error][tid:com.facebook.react.JavaScript] Module RCTLog is not a registered callable module.
Looking at this because I was seeing the same static local asset being fetched over and over from the packager server like @chrisnojima, I found out that this specific behavior was introduced by https://github.com/facebook/react-native/commit/631785f2ee852c4621eed81053a63166887071e6
As @javache was saying, React Native is relying on the system's default NSURLCache.
However the packager is not setting any cache headers for assets requests. Hence in development, everytime a local asset is needed it's fetched again from the packager server.
Adding a Cache-Control header to the packager response for local assets fixes the issue.
I'll send a PR with the fix.
Does this happen with remote images (http://, https://) or with local assets stored in the filesystem that are part of the app?
@sjmueller I understand this happens in release mode, with the packager not running, correct?
I recently have reported multiple issues to NSUrlSession caching in iOS:
- if you provide conditional request headers response would be cached forever
- if server would return must-revalidate header max-age header would be ignored and url caching would for for this request (it would work like if server returned no-cache)
I encountered this bug too. Images are being refetched on every component mount. It doesn't matter if you set caching headers, it omits caching completely. This is happening only with remote images, as far as I can tell, and it is happening in both development and release mode. I'm using RN 0.32.1 on iOS only.
This is happening to my local assets as well. To be more specific, local as in images inside a folder in my JS code, not Images.xcassets.
Having this issue on 0.33.0 in release mode when loading remote images. Every time the component renders it reloads the images. Have a simple ListView with image thumbnails. Downgrading to 0.30.0 "fixes" the issue.
@bsiddiqui I downgraded to [email protected] but this did not seem to solve my issue. Remote images still did not fetch from cache; they are still being loaded from the url. Just an fyi for anyone else attempting to downgrade to resolve this issue, perhaps downgrading further down would do it?
@bestander here's a video of the issue (think it's the same issue)
0.32.1 - see how images flash when ListView re-renders https://www.dropbox.com/s/tmsi8q39w9efkfs/rn32.mov?dl=0
0.31.0 - and previous versions did not have this issue https://www.dropbox.com/s/vf7r9cls2bvklod/rn31.mov?dl=0
Thanks a lot, guys, we hear you. I'll chase this with the team. Any help with pin-pointing which commit did this is very much appreciated.
There are 182 commits between 0.31 and 0.32: https://github.com/facebook/react-native/compare/0.31-stable...0.32-stable Should be doable in a couple of hours and could lead to a much faster fix
@bsiddiqui Can you please link the URL of the images you're trying to load? After https://github.com/facebook/react-native/commit/631785f2ee852c4621eed81053a63166887071e6 we now respect the cache-control headers sent by the server, so if the server is not marking this image as cacheable, we need to refetch it.