Skip to content

fix: clear Metro cache after generating native assets#52

Merged
oblador merged 1 commit intooblador:masterfrom
debitan:fix/clear-metro-cache-after-generate
Mar 24, 2026
Merged

fix: clear Metro cache after generating native assets#52
oblador merged 1 commit intooblador:masterfrom
debitan:fix/clear-metro-cache-after-generate

Conversation

@debitan
Copy link
Copy Markdown
Contributor

@debitan debitan commented Mar 24, 2026

Problem

When SVG file content changes (e.g. after a git rebase), running expo prebuild generates native assets with new content hashes (e.g. icon_svg_220cf9). However, the Metro dev server maintains its own persistent cache from before the change, so at runtime it resolves the old hash (e.g. icon_svg_884cf1). This causes warnings like:

WARN  Could not find image file:///...Neko.app/askneko_svg_884cf1.png

The root cause is that generateAssets spins up its own Metro instance via createMetroServerAndBundleRequestAsync to discover assets, but the dev server's $TMPDIR/metro-cache is a separate persistent cache that doesn't get invalidated.

Fix

Clear $TMPDIR/metro-cache at the end of generateAssets(), after native assets have been written. This forces the next expo start to recompute all asset hashes from scratch, ensuring they match the freshly generated native resources.

The trade-off is a slightly slower first Metro startup after prebuild (cold cache), but it eliminates the hash mismatch class of bugs entirely.

Test plan

  • Confirmed in a production Expo app where a given SVG had hash 884cf1 before rebase and 220cf9 after
  • After applying this patch, expo prebuild clears the Metro cache and subsequent expo start resolves the correct hash

The asset filenames include a content hash (e.g. icon_svg_a1b2c3) that
must match between the native assets and Metro's asset registry. The
generate step spins up its own Metro instance to discover assets, but
the dev server maintains a separate persistent cache.

When SVG content changes (e.g. after a git rebase), the generate step
produces native assets with new hashes, but a stale dev server cache
can still serve the old hash at runtime, causing "Could not find image"
warnings.

Clearing the Metro cache directory after asset generation forces the
dev server to recompute all asset hashes on next start, ensuring they
match the freshly generated native resources.
@oblador oblador merged commit 6f7a774 into oblador:master Mar 24, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants