Skip to content

Commit 7f77017

Browse files
kidrocanecolas
authored andcommitted
[fix] ImageLoader: ImageUriCache is not kept up to date with loaded images
Close #2493
1 parent 1f2aefa commit 7f77017

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

packages/react-native-web/src/exports/Image/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ const BaseImage: ImageComponent = React.forwardRef((props, ref) => {
327327

328328
function abortPendingRequest() {
329329
if (requestRef.current != null) {
330-
ImageLoader.abort(requestRef.current);
330+
ImageLoader.clear(requestRef.current);
331331
requestRef.current = null;
332332
}
333333
}

packages/react-native-web/src/modules/ImageLoader/index.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,13 @@ let id = 0;
7474
const requests = {};
7575

7676
const ImageLoader = {
77-
abort(requestId: number) {
78-
let image = requests[`${requestId}`];
77+
clear(requestId: number) {
78+
const image = requests[`${requestId}`];
7979
if (image) {
8080
image.onerror = null;
8181
image.onload = null;
82-
image = null;
82+
ImageUriCache.remove(image.src);
83+
image.src = '';
8384
delete requests[`${requestId}`];
8485
}
8586
},
@@ -102,7 +103,7 @@ const ImageLoader = {
102103
}
103104
}
104105
if (complete) {
105-
ImageLoader.abort(requestId);
106+
ImageLoader.clear(requestId);
106107
clearInterval(interval);
107108
}
108109
}
@@ -111,7 +112,7 @@ const ImageLoader = {
111112
if (typeof failure === 'function') {
112113
failure();
113114
}
114-
ImageLoader.abort(requestId);
115+
ImageLoader.clear(requestId);
115116
clearInterval(interval);
116117
}
117118
},
@@ -123,6 +124,7 @@ const ImageLoader = {
123124
const image = new window.Image();
124125
image.onerror = onError;
125126
image.onload = (nativeEvent) => {
127+
ImageUriCache.add(uri);
126128
// avoid blocking the main thread
127129
const onDecode = () => {
128130
// Append `source` to match RN's ImageLoadEvent interface
@@ -185,9 +187,8 @@ const ImageLoader = {
185187
ImageLoader.load(
186188
uri,
187189
() => {
188-
// Add the uri to the cache so it can be immediately displayed when used
189-
// but also immediately remove it to correctly reflect that it has no active references
190-
ImageUriCache.add(uri);
190+
// load() adds the uri to the cache so it can be immediately displayed when used,
191+
// but we also immediately remove it to correctly reflect that it has no active references
191192
ImageUriCache.remove(uri);
192193
resolve();
193194
},

0 commit comments

Comments
 (0)