From e2a4c684493c9ee4a3e567809d5b34b1b168c51e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20=C5=BBelawski?= <40713406+tjzel@users.noreply.github.com> Date: Fri, 7 Aug 2026 04:36:19 -0700 Subject: [PATCH] refactor(iOS): persist bundle as local file on par with Android (#57751) Summary: On Android, in development, the JS bundle is downloaded and saved as a file - this is done to avoid passing it through JNI, but iOS could use similar approach for uniform behavior. This allows libraries to be able to obtain the bundle from the file without re-downloading it from Metro. It would impact iOS boot slightly, but only in development, so it should be negligible. ## Changelog: [IOS] [CHANGED] - make iOS preserve dev bundle as a temp file on par with Android implementation Pull Request resolved: https://github.com/react/react-native/pull/57751 Test Plan: rn-tester iOS dev works with this change Reviewed By: cortinico, christophpurrer Differential Revision: D114346244 Pulled By: coado fbshipit-source-id: ebb4264fe8949cd766e71ca0363aa209a61370f7 --- .../React/Base/RCTBundleManager.h | 8 +++++ .../React/Base/RCTJavaScriptLoader.h | 8 +++++ .../React/Base/RCTJavaScriptLoader.mm | 29 ++++++++++++++++++- .../platform/ios/ReactCommon/RCTHost.mm | 12 ++++++-- .../api-snapshots/ReactAppleDebugCxx.api | 2 ++ .../api-snapshots/ReactAppleNewarchCxx.api | 2 ++ .../api-snapshots/ReactAppleReleaseCxx.api | 2 ++ 7 files changed, 60 insertions(+), 3 deletions(-) diff --git a/packages/react-native/React/Base/RCTBundleManager.h b/packages/react-native/React/Base/RCTBundleManager.h index db2c1813e2a..aeef12c40f7 100644 --- a/packages/react-native/React/Base/RCTBundleManager.h +++ b/packages/react-native/React/Base/RCTBundleManager.h @@ -72,5 +72,13 @@ typedef NSMutableArray *_Nullable (^RCTPackagerOptionsUpdater) andDefaultGetter:(nullable RCTBridgelessBundleURLGetter)defaultGetter; - (void)resetBundleURL; @property (nonatomic, nullable) NSURL *bundleURL; + +/** + * File URL of the most recently downloaded bundle persisted to disk. Only set + * when the bundle was fetched from a remote URL (e.g. the packager); nil when + * the bundle was loaded from a local file, in which case its location on disk + * is the bundleURL itself. + */ +@property (nonatomic, nullable) NSURL *downloadedBundleFileURL; @property (nonatomic, nonnull) RCTBundleConfiguration *bundleConfig; @end diff --git a/packages/react-native/React/Base/RCTJavaScriptLoader.h b/packages/react-native/React/Base/RCTJavaScriptLoader.h index f6fcfc58a3b..370af209a8f 100755 --- a/packages/react-native/React/Base/RCTJavaScriptLoader.h +++ b/packages/react-native/React/Base/RCTJavaScriptLoader.h @@ -68,6 +68,14 @@ NS_ENUM(NSInteger){ */ @property (nonatomic, readonly) NSInteger filesChangedCount; +/** + * File URL of the downloaded bundle persisted to disk, from which self.data is + * memory-mapped. Only set when the bundle was fetched from a remote URL (e.g. the + * packager); nil when the bundle was loaded from a local file, in which case the + * bundle's location on disk is self.url. + */ +@property (strong, nonatomic, readonly) NSURL *downloadedBundleFileURL; + @end typedef void (^RCTSourceLoadProgressBlock)(RCTLoadingProgress *progressData); diff --git a/packages/react-native/React/Base/RCTJavaScriptLoader.mm b/packages/react-native/React/Base/RCTJavaScriptLoader.mm index 5f7d6e9b5ef..2abdace3e7a 100755 --- a/packages/react-native/React/Base/RCTJavaScriptLoader.mm +++ b/packages/react-native/React/Base/RCTJavaScriptLoader.mm @@ -25,6 +25,7 @@ NSString *const RCTJavaScriptLoaderErrorDomain = @"RCTJavaScriptLoaderErrorDomai NSData *_data; NSUInteger _length; NSInteger _filesChangedCount; + NSURL *_downloadedBundleFileURL; } @end @@ -210,6 +211,15 @@ static void parseHeaders(NSDictionary *headers, RCTSource *source) source->_filesChangedCount = [headers[@"X-Metro-Files-Changed-Count"] integerValue]; } +static NSURL *persistDownloadedBundle(NSData *data, NSError **error) +{ + NSString *bundlePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"ReactNativeDevBundle.js"]; + if (![data writeToFile:bundlePath options:NSDataWritingAtomic error:error]) { + return nil; + } + return [NSURL fileURLWithPath:bundlePath]; +} + static void attemptAsynchronousLoadOfBundleAtURL( NSURL *scriptURL, RCTSourceLoadProgressBlock onProgress, @@ -314,7 +324,24 @@ static void attemptAsynchronousLoadOfBundleAtURL( } } - RCTSource *source = RCTSourceCreate(sourceURL, data, data.length); + NSError *persistError; + NSURL *downloadedBundleFileURL = persistDownloadedBundle(data, &persistError); + if (downloadedBundleFileURL == nil) { + onComplete(persistError, nil); + return; + } + + NSError *mappingError; + NSData *bundleData = [NSData dataWithContentsOfURL:downloadedBundleFileURL + options:NSDataReadingMappedIfSafe + error:&mappingError]; + if (bundleData == nil) { + onComplete(mappingError, nil); + return; + } + + RCTSource *source = RCTSourceCreate(sourceURL, bundleData, static_cast(bundleData.length)); + source->_downloadedBundleFileURL = downloadedBundleFileURL; parseHeaders(headers, source); onComplete(nil, source); } diff --git a/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTHost.mm b/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTHost.mm index 0d5762dbaf6..815e469ec0c 100644 --- a/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTHost.mm +++ b/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTHost.mm @@ -537,10 +537,18 @@ class RCTHostHostTargetDelegate : public facebook::react::jsinspector_modern::Ho onProgress:(RCTSourceLoadProgressBlock)onProgress onComplete:(RCTSourceLoadBlock)loadCallback { + __weak RCTHost *weakSelf = self; + RCTSourceLoadBlock onComplete = ^(NSError *error, RCTSource *source) { + RCTHost *strongSelf = weakSelf; + if (strongSelf != nil && source != nil) { + strongSelf->_bundleManager.downloadedBundleFileURL = source.downloadedBundleFileURL; + } + loadCallback(error, source); + }; if ([_hostDelegate respondsToSelector:@selector(loadBundleAtURL:onProgress:onComplete:)]) { - [_hostDelegate loadBundleAtURL:sourceURL onProgress:onProgress onComplete:loadCallback]; + [_hostDelegate loadBundleAtURL:sourceURL onProgress:onProgress onComplete:onComplete]; } else { - [RCTJavaScriptLoader loadBundleAtURL:sourceURL onProgress:onProgress onComplete:loadCallback]; + [RCTJavaScriptLoader loadBundleAtURL:sourceURL onProgress:onProgress onComplete:onComplete]; } } diff --git a/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api b/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api index 2f0b741dc71..fa22631658a 100644 --- a/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api @@ -749,6 +749,7 @@ interface RCTBundleConfiguration : public NSObject { interface RCTBundleManager : public NSObject { public @property (assign) NSURL* bundleURL; + public @property (assign) NSURL* downloadedBundleFileURL; public @property (assign) RCTBundleConfiguration* bundleConfig; public virtual _Nullable instancetype initWithBundleConfig:(_Nullable RCTBundleConfiguration* bundleConfig); public virtual void resetBundleURL(); @@ -1818,6 +1819,7 @@ interface RCTSource : public NSObject { public @property (assign, readonly) NSInteger filesChangedCount; public @property (assign, readonly) NSUInteger length; public @property (strong, readonly) NSData* data; + public @property (strong, readonly) NSURL* downloadedBundleFileURL; public @property (strong, readonly) NSURL* url; } diff --git a/scripts/cxx-api/api-snapshots/ReactAppleNewarchCxx.api b/scripts/cxx-api/api-snapshots/ReactAppleNewarchCxx.api index 45383f4e39d..89ee2b1501e 100644 --- a/scripts/cxx-api/api-snapshots/ReactAppleNewarchCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAppleNewarchCxx.api @@ -747,6 +747,7 @@ interface RCTBundleConfiguration : public NSObject { interface RCTBundleManager : public NSObject { public @property (assign) NSURL* bundleURL; + public @property (assign) NSURL* downloadedBundleFileURL; public @property (assign) RCTBundleConfiguration* bundleConfig; public virtual _Nullable instancetype initWithBundleConfig:(_Nullable RCTBundleConfiguration* bundleConfig); public virtual void resetBundleURL(); @@ -1813,6 +1814,7 @@ interface RCTSource : public NSObject { public @property (assign, readonly) NSInteger filesChangedCount; public @property (assign, readonly) NSUInteger length; public @property (strong, readonly) NSData* data; + public @property (strong, readonly) NSURL* downloadedBundleFileURL; public @property (strong, readonly) NSURL* url; } diff --git a/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api b/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api index 6d98f870e97..489144660f7 100644 --- a/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api @@ -749,6 +749,7 @@ interface RCTBundleConfiguration : public NSObject { interface RCTBundleManager : public NSObject { public @property (assign) NSURL* bundleURL; + public @property (assign) NSURL* downloadedBundleFileURL; public @property (assign) RCTBundleConfiguration* bundleConfig; public virtual _Nullable instancetype initWithBundleConfig:(_Nullable RCTBundleConfiguration* bundleConfig); public virtual void resetBundleURL(); @@ -1818,6 +1819,7 @@ interface RCTSource : public NSObject { public @property (assign, readonly) NSInteger filesChangedCount; public @property (assign, readonly) NSUInteger length; public @property (strong, readonly) NSData* data; + public @property (strong, readonly) NSURL* downloadedBundleFileURL; public @property (strong, readonly) NSURL* url; }