Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/react-native/Libraries/Image/ImageSource.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export interface ImageURISource {
* to a URL load request, no attempt is made to load the data from the originating source,
* and the load is considered to have failed.
*
* @platform ios (for `force-cache` and `only-if-cached`)
* @platform ios (for `force-cache`)
*/
cache?: 'default' | 'reload' | 'force-cache' | 'only-if-cached' | undefined;
/**
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native/Libraries/Image/ImageSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export interface ImageURISource {
* to a URL load request, no attempt is made to load the data from the originating source,
* and the load is considered to have failed.
*
* @platform ios (for `force-cache` and `only-if-cached`)
* @platform ios (for `force-cache`)
*/
+cache?: ?('default' | 'reload' | 'force-cache' | 'only-if-cached');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,11 @@ public enum class ImageCacheControl {
* be used to satisfy a URL load request.
*/
RELOAD,
/**
* The existing cache data will be used to satisfy a request, regardless of
* its age or expiration date. If there is no existing data in the cache corresponding
* to a URL load request, no attempt is made to load the data from the originating source,
* and the load is considered to have failed.
*/
ONLY_IF_CACHED,
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ internal class ReactOkHttpNetworkFetcher(private val okHttpClient: OkHttpClient)
ImageCacheControl.RELOAD -> {
cacheControlBuilder.noCache()
}
ImageCacheControl.ONLY_IF_CACHED -> {
cacheControlBuilder.onlyIfCached()
}
ImageCacheControl.DEFAULT -> {
// No-op
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import com.facebook.imagepipeline.image.ImageInfo
import com.facebook.imagepipeline.postprocessors.IterativeBoxBlurPostProcessor
import com.facebook.imagepipeline.request.BasePostprocessor
import com.facebook.imagepipeline.request.ImageRequest
import com.facebook.imagepipeline.request.ImageRequest.RequestLevel
import com.facebook.imagepipeline.request.ImageRequestBuilder
import com.facebook.imagepipeline.request.Postprocessor
import com.facebook.react.bridge.ReactContext
Expand Down Expand Up @@ -311,10 +312,18 @@ public class ReactImageView(
null,
"default" -> ImageCacheControl.DEFAULT
"reload" -> ImageCacheControl.RELOAD
"only-if-cached" -> ImageCacheControl.ONLY_IF_CACHED
else -> ImageCacheControl.DEFAULT
}
}

private fun computeRequestLevel(cacheControl: ImageCacheControl): RequestLevel {
return when (cacheControl) {
ImageCacheControl.ONLY_IF_CACHED -> RequestLevel.DISK_CACHE
else -> RequestLevel.FULL_FETCH
}
}

public fun setDefaultSource(name: String?) {
val newDefaultDrawable = instance.getResourceDrawable(context, name)
if (defaultImageDrawable != newDefaultDrawable) {
Expand Down Expand Up @@ -426,6 +435,7 @@ public class ReactImageView(
val imageSource = this.imageSource ?: return
val uri = imageSource.uri
val cacheControl = imageSource.cacheControl
val requestLevel = computeRequestLevel(cacheControl)

val postprocessorList = mutableListOf<Postprocessor>()
iterativeBoxBlurPostProcessor?.let { postprocessorList.add(it) }
Expand All @@ -445,6 +455,7 @@ public class ReactImageView(
.setResizeOptions(resizeOptions)
.setAutoRotateEnabled(true)
.setProgressiveRenderingEnabled(progressiveRenderingEnabled)
.setLowestPermittedRequestLevel(requestLevel)

if (resizeMethod == ImageResizeMethod.NONE) {
imageRequestBuilder.setDownsampleOverride(DownsampleMode.NEVER)
Expand Down
21 changes: 18 additions & 3 deletions packages/rn-tester/js/examples/Image/ImageExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,20 @@ function CacheControlAndroidExample(): React.Node {
key={reload}
/>
</View>
<View style={styles.leftMargin}>
<RNTesterText style={styles.resizeModeText}>
Only-if-cached
</RNTesterText>
<Image
source={{
uri: fullImage.uri + '?cacheBust=only-if-cached',
cache: 'only-if-cached',
}}
style={styles.base}
key={reload}
onError={e => console.log(e.nativeEvent.error)}
/>
</View>
</View>

<View style={styles.horizontal}>
Expand Down Expand Up @@ -1091,9 +1105,10 @@ exports.examples = [
},
{
title: 'Cache Policy',
description: ('First image will be loaded and will be cached. ' +
'Second image is the same but will be reloaded if re-rendered ' +
'as the cache policy is set to reload.': string),
description: `- First image will be loaded and cached.
- Second image is the same but will be reloaded if re-rendered as the cache policy is set to reload.
- Third image will never be loaded as the cache policy is set to only-if-cached and the image has not been loaded before.
`,
render: function (): React.Node {
return <CacheControlAndroidExample />;
},
Expand Down
Loading