A static asset is a file that ships in the native app binary, outside the JavaScript bundle: images, videos, sounds, SQLite databases and fonts are the common ones. In development, local assets are served over HTTP, and production builds bundle them into the binary. expo-asset gives you two routes for these files, embedding them in the native project at build time with its config plugin, or loading them at runtime from JavaScript.
Key terms
- Static asset: a file bundled with the native app binary instead of the JavaScript bundle.
assets: the expo-asset config plugin property, an array of files or directories to link to the native project.useAssets: an expo-asset hook that loads assets and returns them, or the loading error, as component state.Asset: the expo-asset class for one asset, with fields such asname,localUrianddownloaded.localUri: theAssetfield that holds where the asset's data sits on the device, usually afile://URI, andnulluntil the asset is downloaded.
Embedding files with the config plugin
Install the package with npx expo install expo-asset (the expo package already depends on it). Then add the plugin to the plugins array of your app config with an assets property. The app config shown below links a whole directory of badge images and one sound file. Each path is relative to the project root, where the app config lives.
Embedded files take their native resource names from their file names, whether you list a file directly or through a directory. After changing the list, make a new development build. In code, an embedded image is referenced by that name as the uri, without require and without the file extension, and it needs an explicit size: <Image source={{ uri: 'gold_medal' }} style={{ width: 64, height: 64 }} /> renders assets/badges/gold_medal.png.
The plugin accepts images (.png, .jpg, .gif), media (.mp4, .mp3, .lottie, .riv), .db SQLite files, .glb 3D models and .json files. Font files are skipped with a warning telling you to use expo-font instead, and any other unsupported extension is skipped with a "not a supported asset type" warning. On Android, a file name may contain only lowercase a to z, digits and underscores, must start with a letter, and can't be a Java reserved word such as new. Other names are skipped with a warning. That's why the badge is gold_medal.png and not Gold Medal.png.
Loading at runtime with useAssets
For a file format the plugin doesn't support, load the file at runtime with useAssets from expo-asset. It takes one require() module id or an array of them, downloads the assets and stores them locally, then returns a tuple [Asset[] | undefined, Error | undefined]. The first element stays undefined until every asset has loaded, and the second stays undefined unless loading failed.
That shape differs from useFonts, which returns [boolean, Error | null]. With useAssets, the loaded check is whether assets is defined, which is what the BadgeShelf component tests before rendering its badges. The assets guide labels its example src/app/index.tsx but requires ./assets/images/example.png, a path that doesn't match the template's layout, where assets/ sits at the project root and routes live in src/app/.
The hook loads once. Internally it calls Asset.loadAsync a single time in an effect, so changing the list you pass later doesn't reload anything.
A file:// path from Asset.loadAsync
Outside a component, or when another library needs a file on disk, use the Asset class directly. Asset.loadAsync accepts a require() value or a network URL, or an array of either, and resolves to an array of Asset instances once they are saved to disk. It's a wrapper around Asset.fromModule(module).downloadAsync().
Before the download, localUri is null, and the downloaded flag tells you whether downloadAsync has finished. For an asset from require(), name is the file name without the extension and without any @ scale suffix. An asset created from a network URL has an empty name on Android and iOS.
On Android and iOS, downloadAsync copies data it fetches over HTTP, such as a network URL or a local asset in development, into a file in the device's cache directory, and localUri becomes the file:// URI of that copy. When the asset has a known hash, as bundled assets do, it skips the download if the cached copy matches that hash. An asset from a network URL has no hash, so any cached copy is reused as is, even if the remote file has changed. The OS may clear that directory, so the file isn't guaranteed to survive between app sessions. The getShareCardPath helper downloads a share card image from a URL and calls Asset.loadAsync whenever it needs the path, instead of keeping a URI from an earlier session.
Bundled files in a release build don't always get a cache copy. A URI that is already a file:// URL usually comes back unchanged, so localUri points at that file instead of a copy in the cache. On Android, a bundled image's localUri can be its drawable resource name rather than a file:// path. expo-asset keeps it equal to uri so React Native's Image can load it. On web, nothing is copied: localUri is set to the asset's URL.
Code Examples
{
"expo": {
"plugins": [
[
"expo-asset",
{
"assets": ["./assets/badges", "./assets/sounds/level_up.mp3"]
}
]
]
}
}import { useAssets } from 'expo-asset';
import { Image } from 'expo-image';
import { StyleSheet, Text, View } from 'react-native';
export function BadgeShelf() {
const [assets, error] = useAssets([
require('@/assets/images/badge-bronze.png'),
require('@/assets/images/badge-silver.png'),
]);
if (error) {
return <Text>Could not load the badges.</Text>;
}
// undefined until every asset has loaded
if (!assets) {
return null;
}
return (
<View style={styles.shelf}>
{assets.map((asset) => (
<Image key={asset.name} source={asset} style={styles.badge} />
))}
</View>
);
}
const styles = StyleSheet.create({
shelf: { flexDirection: 'row', gap: 12 },
badge: { width: 64, height: 64 },
});import { Asset } from 'expo-asset';
export async function getShareCardPath(cardUrl: string): Promise<string> {
const [shareCard] = await Asset.loadAsync(cardUrl);
if (!shareCard.localUri) {
throw new Error(`No local copy of ${cardUrl}`);
}
// On Android and iOS, the file:// URI of the copy in the cache directory
return shareCard.localUri;
}