Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion: A more reliable way to check if image is in the cache #2708

Open
1 task done
dominictb opened this issue Aug 16, 2024 · 0 comments · May be fixed by #2709
Open
1 task done

Suggestion: A more reliable way to check if image is in the cache #2708

dominictb opened this issue Aug 16, 2024 · 0 comments · May be fixed by #2709
Labels
enhancement Requires extension or creation of new React Native API

Comments

@dominictb
Copy link

Is there an existing request?

  • I have searched for this request

Describe the feature request

Problem

Original issue: Expensify/App#45853

Most of the time if the image is loaded once, it will be in the cache, and subsequent fetch for the same image URI will hit the cache. However, this implementation

const [state, updateState] = React.useState(() => {
const uri = resolveAssetUri(source);
if (uri != null) {
const isLoaded = ImageLoader.has(uri);
if (isLoaded) {
return LOADED;
}
}
return IDLE;
});

indicate that if the initial state of the image will always be IDLE even if the image is loaded before.

Solution

We will modify the ImageLoader.has logic to be able to check reliably if the image is in the cache. It works both when the image URI is previously loaded by <img tag or by the Image component in react-native-web

let globalImageToCheckCache = new window.Image()
ImageLoader.has = (uri) => {
   globalImageToCheckCache.src = uri;
   let isInCache = globalImageToCheckCache.complete
    globalImageToCheckCache.src  = '' // unassign to avoid firing extra request
    return isInCache
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Requires extension or creation of new React Native API
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant