react-native icon indicating copy to clipboard operation
react-native copied to clipboard

android: `<Image>` component caches `file://...` even when it has been deleted

Open msbit opened this issue 1 year ago • 0 comments

Description

Under Android:

  • showing a file://... URI using <Image>
  • deleting the underlying file
  • reshowing it using a new <Image> component (hence newly mounted)

causes the image to still be shown, even though it no longer exists on disk.

This contrasts with iOS which will not show the image the second time. This behaviour is, I believe, the correct one, and indicates that the Android code is performing some additional, unexpected caching.

Steps to reproduce

App.tsx:

import { Component } from 'react';
import { Image, StyleSheet } from 'react-native';

import { DocumentDirectoryPath, unlink, writeFile } from 'react-native-fs';

export default class extends Component {
  constructor(props) {
    super(props);

    this.state = {
      show: false,
      uri: `file://${DocumentDirectoryPath}/image.jpg`,
    };
  }

  async componentDidMount() {
    //  create file
    await writeFile(this.state.uri, data, 'base64');
    //  show it
    await stateApplied(this, { show: true });
    //  wait for a bit
    await elapsed(500);

    //  remove the file
    await unlink(this.state.uri);
    //  hide it
    await stateApplied(this, { show: false });
    //  wait for a bit
    await elapsed(500);

    //  show the file
    await stateApplied(this, { show: true });
  }

  render() {
    const { show, uri } = this.state;
    return show ?
      <Image
        source={{ uri }}
        style={styles.image}
      />
      : undefined;
  }
};

const data = '/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQoHBwYIDAoMDAsKCwsNDhIQDQ4RDgsLEBYQERMUFRUVDA8XGBYUGBIUFRT/2wBDAQMEBAUEBQkFBQkUDQsNFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBT/wAARCAAgACADAREAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4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yozclAOgNe5kmTY/CY6FavG0Vf7S7PzPmOJeIsqx+WVMPhZ3m+W3uyW0k+q7HdR/tL/DKC3j8zVym1QCW0+bA/8AHK8R8O5rKTtD/wAmX+Z9LHi/IoxXNU/8ll/kcH8efjZ4J8afC7VtH0S+e51K4aHyohYypnEqseSgA4Br3MjyfH4PH061eNoq99U+j8z5jifiLKswyqrhsLO83ay5ZLZp9UN+O3x48B+Mfhhquk6NqZudRmeExxizlTIWVGPJUDoDTyTI8xwmOhWrwtFX6rs/MXEvE+T4/K6mHwtS83y2XK1tJPqux3dv+078MEgiVtawwUAj7BN6f7leJLhvNnJtU/8AyZf5n0sOMsgUUnW/8ll/kcJ8dfjz4D8Y/DLVNK0bUzc6hM8BjjFnKmQsqM3JUDoDXt5LkeY4THQrV4Wir9V2fmfNcS8T5Pj8sqYfC1LzbjZcrW0k307He237TvwwCQodZ+YBRj7BN1/74rxXw3mzlf2f/ky/zPpY8ZZAopOt/wCSy/yP/9k=';

const styles = StyleSheet.create({
  image: {
    borderColor: 'red',
    borderWidth: 2,
    height: 128,
    width: 128,
  },
});

function elapsed(ms) {
  return new Promise(r => setTimeout(r, ms));
}

function stateApplied(component, updater) {
  return new Promise(r => component.setState(updater, r));
}

React Native Version

0.74.2

Affected Platforms

Runtime - Android

Output of npx react-native info

info Fetching system and libraries information...
System:
  OS: macOS 14.5
  CPU: (12) arm64 Apple M2 Pro
  Memory: 2.22 GB / 32.00 GB
  Shell:
    version: 5.2.26
    path: /opt/local/bin/bash
Binaries:
  Node:
    version: 18.20.2
    path: /opt/local/bin/node
  Yarn:
    version: 3.6.4
    path: ~/bin/yarn
  npm:
    version: 9.9.3
    path: /opt/local/bin/npm
  Watchman:
    version: 2024.05.06.00
    path: /opt/homebrew/bin/watchman
Managers:
  CocoaPods:
    version: 1.15.2
    path: /Users/tom/.rbenv/shims/pod
SDKs:
  iOS SDK:
    Platforms:
      - DriverKit 23.4
      - iOS 17.4
      - macOS 14.4
      - tvOS 17.4
      - visionOS 1.1
      - watchOS 10.4
  Android SDK: Not Found
IDEs:
  Android Studio: 2023.3 AI-233.14808.21.2331.11709847
  Xcode:
    version: 15.3/15E204a
    path: /usr/bin/xcodebuild
Languages:
  Java:
    version: 17.0.11
    path: /usr/bin/javac
  Ruby:
    version: 3.1.5
    path: /Users/tom/.rbenv/shims/ruby
npmPackages:
  "@react-native-community/cli": Not Found
  react:
    installed: 18.2.0
    wanted: 18.2.0
  react-native:
    installed: 0.74.2
    wanted: 0.74.2
  react-native-macos: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: true
  newArchEnabled: false
iOS:
  hermesEnabled: true
  newArchEnabled: false

Stacktrace or Logs

-

Reproducer

https://github.com/msbit/image-cache-repro

Screenshots and Videos

https://github.com/facebook/react-native/assets/1694533/4304e351-0b0f-4d73-b0f6-6b5ff2bc17a1

msbit avatar Jun 26 '24 05:06 msbit