Skip to content

Commit 0971652

Browse files
Abbondanzofacebook-github-bot
authored andcommitted
Implement resizeMethod="resize" for image prefetching (#54228)
Summary: ## 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] Differential Revision: D85200818
1 parent 5fcbd64 commit 0971652

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,30 @@ 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+
bool shouldResize = false;
81+
const auto& resizeMethod = imageProps.resizeMethod;
82+
if (resizeMethod == "auto") {
83+
// Only resize for local content/file URIs
84+
const auto& uri = newImageSource.uri;
85+
shouldResize =
86+
uri.starts_with("content://") || uri.starts_with("file://");
87+
88+
} else if (resizeMethod == "resize") {
89+
shouldResize = true;
90+
}
91+
92+
// If we would resize but have no dimensions, skip creating the request
93+
if (shouldResize &&
94+
(newImageSource.size.width == 0 || newImageSource.size.height == 0)) {
95+
// Keep the old state - don't create a new image request
96+
return;
97+
}
98+
}
99+
#endif
100+
77101
ImageState state{
78102
newImageSource,
79103
imageManager_->requestImage(

0 commit comments

Comments
 (0)