[Remote Cache] How to get consistent cache keys between metro server and a bundle
I'm configuring Metro to use a remote cache (https://metrobundler.dev/docs/caching/) stored in S3 to speed up local development by sharing CI-generated bundles. The intended workflow is:
- Build bundle on CI on every merge to master
- During the build, populate cache in S3
- Then, when developers runs the app locally, they should be able to download the cache from S3 first, instead building the whole app locally.
I created a simple PoC / playground to demonstrate the problem: https://github.com/adammruk/metro-remote-cache-example
The overall mechanism works correctly. You can verify this by running npm run build twice in the example project:
- The first run takes longer and populates the cache in S3.
- The second run is significantly faster and shows a 100% cache hit rate, confirming that the remote cache is functional.
Issue:
The problem occurs when using the metro bundle command to populate the cache and then attempting to reuse it with the metro start server. Metro does attempt to use the remote cache when running via metro start, but reports a 0% cache hit rate. I verified the following:
- Both bundle and start processes generate the same number of cache keys.
- The number of keys matches the number of bundled modules.
However, the actual cache keys differ, which causes cache misses.
To debug this, I inspected the URL the app uses to fetch the bundle from metro server: http://localhost:8081/index.bundle?platform=ios&dev=true&lazy=false&minify=false&inlineSourceMap=false&modulesOnly=false&runModule=true&excludeSource=true&sourcePaths=url-server
This gives some insight into the server config, but I haven't been able to generate identical bundles (or matching cache keys) via metro bundle using similar flags.
Expected behavior: My core question is: Can I use the metro bundle command to populate the remote cache in a way that's fully compatible with what metro start consumes? If so, how can I ensure that both methods produce identical cache keys so that the remote cache is reusable between them?