Skip to content

Commit 26ad949

Browse files
Abbondanzometa-codesync[bot]
authored andcommitted
Implement resizeMethod="resize" for image prefetching (#54228)
Summary: Pull Request resolved: #54228 ## Issue When prefetching images with the `resizeMethod` prop set, there's a brief period of time where the image has not laid out and the dimensions are 0x0. Since `ImageSource.h` only considers the `type` and `uri` properties for equality, two different `ImageSource` objects would be considered equal despite having different `size`. We would continue on to prefetch the image in its fullest quality and retain that image in `ImageState`, and upon subsequent requests once we have non-zero layouts, we would bail out early in the `ImageShadowNode::updateStateIfNeeded` method since both the old image source and image request params are equal in this case. ## Fix Rather than adding `size` to the equality check, this change adds identical logic directly from `ReactImageView` to determine if we should postpone the image request: if the image is resizable and we don't have a width or a height, postpone! ## Additional Context I noodled with a few spots of where this should ultimately live, but this seemed like the least invasive without making some larger refactors. The other approach I considered was to instead return an optional `ImageRequest` object from `ImageManager->requestImage call` to signal that no request was ever made, and using the `ImageRequest` response as part of the equality check. Changelog: [Internal] Reviewed By: christophpurrer Differential Revision: D85200818 fbshipit-source-id: 82090feb500dafa47af14220a17262d715d20a7a
1 parent b5e2091 commit 26ad949

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

packages/react-native/ReactCommon/react/renderer/components/image/ImageShadowNode.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,24 @@ void ImageShadowNode::updateStateIfNeeded() {
7474
return;
7575
}
7676

77+
#ifdef ANDROID
78+
// Check if we should skip prefetching based on shouldResize logic
79+
if (ReactNativeFeatureFlags::enableImagePrefetchingAndroid()) {
80+
const auto& resizeMethod = imageProps.resizeMethod;
81+
const auto& uri = newImageSource.uri;
82+
bool shouldResize = (resizeMethod == "resize") ||
83+
// Only resize for local content/file URIs
84+
(resizeMethod == "auto" &&
85+
(uri.starts_with("content://") || uri.starts_with("file://")));
86+
// If we would resize but have no dimensions, skip creating the request
87+
if (shouldResize &&
88+
(newImageSource.size.width == 0 || newImageSource.size.height == 0)) {
89+
// Keep the old state - don't create a new image request
90+
return;
91+
}
92+
}
93+
#endif
94+
7795
ImageState state{
7896
newImageSource,
7997
imageManager_->requestImage(

0 commit comments

Comments
 (0)