Skip to content

Commit

Permalink
Prevent image loading placeholder for cached images (#669)
Browse files Browse the repository at this point in the history
Give the image time to get loaded from the cache before setting the loading state
  • Loading branch information
schroda authored Mar 24, 2024
1 parent 6186cb0 commit a96038b
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/components/util/SpinnerImage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export const SpinnerImage = forwardRef((props: IProps, imgRef: ForwardedRef<HTML
useEffect(() => {
let tmpImageSourceUrl: string;
const imageRequest = requestManager.requestImage(src);
let cacheTimeout: NodeJS.Timeout;

const fetchImage = async () => {
try {
Expand All @@ -60,7 +61,12 @@ export const SpinnerImage = forwardRef((props: IProps, imgRef: ForwardedRef<HTML
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;

if (isImageCached) {
Expand All @@ -82,6 +88,7 @@ export const SpinnerImage = forwardRef((props: IProps, imgRef: ForwardedRef<HTML
if (tmpImageSourceUrl) {
URL.revokeObjectURL(tmpImageSourceUrl);
}
clearTimeout(cacheTimeout);
imageRequest.abortRequest(new Error('Component was unmounted'));
};
}, [imgLoadRetryKey]);
Expand Down

0 comments on commit a96038b

Please sign in to comment.