From 7fe7a2a26b748ea9d115b1a04d4d4e187b6f6a05 Mon Sep 17 00:00:00 2001 From: Nick Lockwood Date: Thu, 8 Oct 2015 12:28:38 -0700 Subject: [PATCH] Improved JS bundle loading performance MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: @​public RCTJavaScriptLoader was using an NSURLSessionDataTask to load local bundle.js files. While this works, it was non-optimal from a performance point of view. Reviewed By: @tadeuzagallo Differential Revision: D2522598 fb-gh-sync-id: b32981b3be4c336512d1462d3f4943b5aad34ae0 --- React/Base/RCTJavaScriptLoader.m | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/React/Base/RCTJavaScriptLoader.m b/React/Base/RCTJavaScriptLoader.m index abc71b201a..5de25cb255 100755 --- a/React/Base/RCTJavaScriptLoader.m +++ b/React/Base/RCTJavaScriptLoader.m @@ -23,15 +23,26 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init) // Sanitize the script URL scriptURL = [RCTConvert NSURL:scriptURL.absoluteString]; - if (!scriptURL || - (scriptURL.fileURL && ![[NSFileManager defaultManager] fileExistsAtPath:scriptURL.path])) { + if (!scriptURL) { NSError *error = [NSError errorWithDomain:@"JavaScriptLoader" code:1 userInfo:@{ - NSLocalizedDescriptionKey: scriptURL ? [NSString stringWithFormat:@"Script at '%@' could not be found.", scriptURL] : @"No script URL provided" + NSLocalizedDescriptionKey: @"No script URL provided." }]; onComplete(error, nil); return; } + // Load local script file + if (scriptURL.fileURL) { + NSString *filePath = scriptURL.resourceSpecifier; + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ + NSError *error = nil; + NSString *rawText = [NSString stringWithContentsOfFile:filePath usedEncoding:NULL error:&error]; + onComplete(error, rawText); + }); + return; + } + + // Load remote script file NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithURL:scriptURL completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) {