Skip to content

[metro-cache] metro-cache preview#4576

Draft
zhongwuzw wants to merge 12 commits intomodule-federation:mainfrom
zhongwuzw:features/metro-cache
Draft

[metro-cache] metro-cache preview#4576
zhongwuzw wants to merge 12 commits intomodule-federation:mainfrom
zhongwuzw:features/metro-cache

Conversation

@zhongwuzw
Copy link

Description

Introduces @module-federation/metro-cache, a new React Native native module that provides persistent on-disk caching for federated remote bundles. This eliminates redundant network downloads on app cold starts by caching bundles locally and validating them against manifest hashes (SHA-256).

What's new

@module-federation/metro-cache (new package)

  • Native module with iOS (Objective-C++) and Android (Kotlin) implementations
  • NativeMFECache API: downloadFile, evaluateJavaScript, deleteFile, getFileSize, getDocumentDirectory
  • CacheManager: disk-backed cache index with LRU eviction, host build hash invalidation, and disk manifest persistence
  • Configurable maxCacheSizeBytes, maxAgeMs, minCacheSizeBytes
  • CocoaPods podspec and Android Gradle build configuration
  • React Native autolinking via react-native.config.js

@module-federation/metro (metro-core) changes

  • asyncRequire.ts: intercepts bundle loads with a cache layer — checks manifest hash → cache hit (eval from file) / cache miss (download + verify SHA-256 + persist) / fallback to network on mismatch
  • Inflight deduplication for concurrent loads of the same bundle
  • cache-interface.ts: ICacheManager / ICacheNative interfaces for loose coupling between metro-core and metro-cache (graceful degradation when metro-cache is not installed)
  • metroCorePlugin.ts: afterResolve hook extracts bundle hashes from manifest (metaData.buildInfo.hash, exposes[].hash, shared[].hash) into a global __MFE_BUNDLE_HASHES__ map consumed by the cache layer
  • bundle-remote/index.ts: hash computation during production bundle builds

Cache flow

bundle request → check MFE_BUNDLE_HASHES for expected hash
├─ no hash → fallback to loadBundleAsync (no cache)
└─ has hash → check CacheManager
├─ cache HIT + hash match → eval from local file
└─ cache MISS or hash mismatch → downloadFile → verify SHA-256
├─ match → persist to cache + eval from file
└─ mismatch → delete file + fallback to loadBundleAsync

Related Issue

Types of changes

  • Docs change / refactoring / dependency upgrade
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

Checklist

  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • I have updated the documentation.

@netlify
Copy link

netlify bot commented Mar 20, 2026

Deploy Preview for module-federation-docs failed. Why did it fail? →

Name Link
🔨 Latest commit 1a85880
🔍 Latest deploy log https://app.netlify.com/projects/module-federation-docs/deploys/69bd67012861120008f6f54d

@changeset-bot
Copy link

changeset-bot bot commented Mar 20, 2026

⚠️ No Changeset found

Latest commit: 1a85880

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

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.

1 participant