From 1586a32fa739b0ac918b5877ea3df4c6d0022729 Mon Sep 17 00:00:00 2001 From: Anoop Chaurasiya Date: Wed, 25 May 2016 10:17:36 -0700 Subject: [PATCH] add support to provide fallback-sourceURL: in case primary-sourceURL fails to load Reviewed By: javache Differential Revision: D3339692 fbshipit-source-id: 93fa1821bf4abca878832d4f75c6b9968d8d0460 --- React/Base/RCTBatchedBridge.m | 13 ++++++++++++- React/Base/RCTBridgeDelegate.h | 9 +++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/React/Base/RCTBatchedBridge.m b/React/Base/RCTBatchedBridge.m index 4b210e072..ac3cd7e80 100644 --- a/React/Base/RCTBatchedBridge.m +++ b/React/Base/RCTBatchedBridge.m @@ -170,7 +170,18 @@ RCT_EXTERN NSArray *RCTGetModuleClasses(void); if ([self.delegate respondsToSelector:@selector(loadSourceForBridge:withBlock:)]) { [self.delegate loadSourceForBridge:_parentBridge withBlock:onSourceLoad]; } else if (self.bundleURL) { - [RCTJavaScriptLoader loadBundleAtURL:self.bundleURL onComplete:onSourceLoad]; + [RCTJavaScriptLoader loadBundleAtURL:self.bundleURL onComplete:^(NSError *error, NSData *source) { + if (error && [self.delegate respondsToSelector:@selector(fallbackSourceURLForBridge:)]) { + NSURL *fallbackURL = [self.delegate fallbackSourceURLForBridge:_parentBridge]; + if (fallbackURL && ![fallbackURL isEqual:self.bundleURL]) { + RCTLogError(@"Failed to load bundle(%@) with error:(%@)", self.bundleURL, error.localizedDescription); + self.bundleURL = fallbackURL; + [RCTJavaScriptLoader loadBundleAtURL:self.bundleURL onComplete:onSourceLoad]; + return; + } + } + onSourceLoad(error, source); + }]; } else { // Allow testing without a script dispatch_async(dispatch_get_main_queue(), ^{ diff --git a/React/Base/RCTBridgeDelegate.h b/React/Base/RCTBridgeDelegate.h index d8606c8d9..8d85f78b7 100644 --- a/React/Base/RCTBridgeDelegate.h +++ b/React/Base/RCTBridgeDelegate.h @@ -23,6 +23,15 @@ typedef void (^RCTSourceLoadBlock)(NSError *error, NSData *source); @optional +/** + * The bridge will attempt to load the JS source code from the location specified + * by the `sourceURLForBridge:` method, if loading fails, you can implement this + * method to specify fallbackSourceURL. + * NOTE: We don't plan to support this API permanently (this method will be + * removed after we track down why a valid sourceURL fails to load sometimes). + */ +- (NSURL *)fallbackSourceURLForBridge:(RCTBridge *)bridge; + /** * The bridge initializes any registered RCTBridgeModules automatically, however * if you wish to instantiate your own module instances, you can return them