Caching and Lists with expo-image

+15 Mana ✨

Lists of images are where loading behavior gets noticed: photos download again when you scroll back, fade in every time a row reappears, or a reused row shows the previous item's picture for a moment. expo-image caches images on disk by default, and the rest of its list behavior is set with props and static methods on Image.

Key terms

  • cachePolicy: the Image prop that controls whether and where a loaded image is cached.
  • cacheKey: the key an image is stored and looked up under, which is its uri unless you set one.
  • recyclingKey: a string prop whose change resets the view to blank or to the placeholder before the next image loads.
  • Image.prefetch: a static method that loads images by URL ahead of time, for an Image to show later.
  • skipOnCacheHit: a transition option that skips the animation when an image first appears from a cache hit.

Where cachePolicy stores images

cachePolicy takes one of four values, and the default is 'disk':

  • 'none' doesn't cache the image at all.
  • 'disk' reads the image from the disk cache when it's there, and otherwise downloads it and stores it on disk.
  • 'memory' caches it in memory, which may be purged very quickly to prevent high memory use.
  • 'memory-disk' caches it in memory, with a fallback to the disk cache.

The memory cache lasts only while the app runs. The disk cache sits in the cache directory, where the OS may delete files when storage runs low. To see which cache served an image, read cacheType in the onLoad event: it's 'none', 'disk' or 'memory'.

When a new photo keeps the old URL

On Android and iOS, the disk cache ignores the Cache-Control and Expires headers of the image response. Android keeps up to 250 MB of cached images, evicts the least recently used ones beyond that, and never expires them. iOS removes images that haven't been read for a week and has no disk size limit unless you pass maxDiskSize to Image.configureCache(), an iOS-only method. On web, the browser's HTTP cache stores the image and follows its headers.

So when a user uploads a new avatar to the same URL, the app keeps showing the cached one. Add a version query parameter to the URL, or give the source a new cacheKey, for example one built from the avatar's version number. Without a cacheKey the uri is the key, which is why a changed URL loads the new file. Image.clearDiskCache() and Image.clearMemoryCache() empty the caches, and cachePolicy="none" skips them.

A reused row shows someone else's photo

In a recycling list such as FlashList, the image view that showed one row is reused for another, and until the new image loads it can still show the previous source. Changing recyclingKey resets the view to blank, or to the placeholder, before the new image loads. Give it a value unique to the item, such as recyclingKey={item.id}. It's a string, defaults to null, and works on Android and iOS only.

Changing cachePolicy doesn't help with this symptom. The policy decides whether and where images are stored, and the stale picture is what the reused view still displays from its previous row.

skipOnCacheHit

A fade that looks good on a first download gets tiresome when it replays every time a cached row scrolls back into view. New in SDK 58, the transition object accepts skipOnCacheHit:

  • 'none' always plays the transition, and is the default.
  • 'memory' skips it for images served from the in-memory cache.
  • 'all' skips it for any cache hit, so only uncached loads animate.

Locally bundled assets count as in-memory cache hits, so both 'memory' and 'all' skip their transition. The option only affects an image's first appearance in a view: a later source change on a populated view still plays the transition, and a visible placeholder still counts as the first appearance. Like recyclingKey, it's Android and iOS only.

The ContactRow component combines the two props, recyclingKey={item.id} and transition={{ duration: 200, skipOnCacheHit: 'all' }}, so an avatar fades in on its first download and appears instantly when its row comes back. It also sets cachePolicy="memory-disk", which caches avatars in memory with a fallback to the disk cache.

Fetching ahead with Image.prefetch

Image.prefetch(urls, options) takes one URL or an array, with optional options, and returns Promise<boolean>. It resolves to true once every image is prefetched, and to false as soon as one fails, without waiting for the others. The options' cachePolicy defaults to 'memory-disk', unlike the component's 'disk' default, and accepts 'disk', 'memory-disk' or 'memory', but not 'none'. A headers option sends HTTP headers with the requests. The second argument can also be the cache policy as a plain string, such as Image.prefetch(urls, 'disk').

The warmGallery helper prefetches the photos of a gallery's next page and warns when any of them failed. Because prefetched images go to memory and disk by default, the Image that later shows them should use 'disk' or 'memory-disk'.

Code Examples

tsx
import { Image } from 'expo-image';
import { StyleSheet, Text, View } from 'react-native';

type Contact = { id: string; name: string; avatarUrl: string };

// Rendered as the row of a recycling list such as FlashList
export function ContactRow({ item }: { item: Contact }) {
  return (
    <View style={styles.row}>
      <Image
        source={item.avatarUrl}
        recyclingKey={item.id}
        cachePolicy="memory-disk"
        transition={{ duration: 200, skipOnCacheHit: 'all' }}
        style={styles.avatar}
      />
      <Text style={styles.name}>{item.name}</Text>
    </View>
  );
}

const styles = StyleSheet.create({
  row: { flexDirection: 'row', alignItems: 'center', gap: 12, padding: 12 },
  avatar: { width: 48, height: 48, borderRadius: 24 },
  name: { fontSize: 16 },
});
typescript
import { Image } from 'expo-image';

export async function warmGallery(photoUrls: string[]) {
  // Resolves to false as soon as one image fails
  const allLoaded = await Image.prefetch(photoUrls, { cachePolicy: 'memory-disk' });
  if (!allLoaded) {
    console.warn('At least one gallery photo failed to prefetch');
  }
}
✓ Completed