Prevent image loading placeholder for cached images (#669)
Give the image time to get loaded from the cache before setting the loading state
This commit is contained in:
@@ -49,6 +49,7 @@ export const SpinnerImage = forwardRef((props: IProps, imgRef: ForwardedRef<HTML
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
let tmpImageSourceUrl: string;
|
let tmpImageSourceUrl: string;
|
||||||
const imageRequest = requestManager.requestImage(src);
|
const imageRequest = requestManager.requestImage(src);
|
||||||
|
let cacheTimeout: NodeJS.Timeout;
|
||||||
|
|
||||||
const fetchImage = async () => {
|
const fetchImage = async () => {
|
||||||
try {
|
try {
|
||||||
@@ -60,7 +61,12 @@ export const SpinnerImage = forwardRef((props: IProps, imgRef: ForwardedRef<HTML
|
|||||||
tmpImageSourceUrl = image;
|
tmpImageSourceUrl = image;
|
||||||
};
|
};
|
||||||
|
|
||||||
const checkCache = await Promise.race([imageRequest.response, Promise.resolve(false)]);
|
const checkCache = await Promise.race([
|
||||||
|
imageRequest.response,
|
||||||
|
new Promise((resolve) => {
|
||||||
|
cacheTimeout = setTimeout(resolve, 50);
|
||||||
|
}),
|
||||||
|
]);
|
||||||
const isImageCached = !!checkCache;
|
const isImageCached = !!checkCache;
|
||||||
|
|
||||||
if (isImageCached) {
|
if (isImageCached) {
|
||||||
@@ -82,6 +88,7 @@ export const SpinnerImage = forwardRef((props: IProps, imgRef: ForwardedRef<HTML
|
|||||||
if (tmpImageSourceUrl) {
|
if (tmpImageSourceUrl) {
|
||||||
URL.revokeObjectURL(tmpImageSourceUrl);
|
URL.revokeObjectURL(tmpImageSourceUrl);
|
||||||
}
|
}
|
||||||
|
clearTimeout(cacheTimeout);
|
||||||
imageRequest.abortRequest(new Error('Component was unmounted'));
|
imageRequest.abortRequest(new Error('Component was unmounted'));
|
||||||
};
|
};
|
||||||
}, [imgLoadRetryKey]);
|
}, [imgLoadRetryKey]);
|
||||||
|
|||||||
Reference in New Issue
Block a user