Skip to content

Commit a9ec420

Browse files
okwasniewskihuntie
authored andcommitted
fix(iOS) [0.74]: RCTRedBox not appearing in Bridgeless when metro is not running (#43147)
Summary: When testing out `0.74.0-rc0` I found that when the metro is not running we are not displaying RedBox which bumps users to start the packager and reload the app. It also fixes the case where users try to reload by clicking the "Reload" button on RedBox. ## Before https://github.com/facebook/react-native/assets/52801365/086c557f-ea1f-4a97-b4c7-df8a945cc7a0 ## After https://github.com/facebook/react-native/assets/52801365/9f8421b3-5e83-466f-8cdb-38f97981275d ## Changelog: [IOS] [FIXED] - RCTRedBox not appearing in Bridgeless when metro is not running Pull Request resolved: #43147 Test Plan: Build the app without metro running check if RedBox is shown Reviewed By: javache Differential Revision: D54632056 Pulled By: dmytrorykun fbshipit-source-id: fb6742898d3bd82545bfffd9175208e1a5984cb6
1 parent bd39897 commit a9ec420

2 files changed

Lines changed: 44 additions & 7 deletions

File tree

packages/react-native/React/CoreModules/RCTRedBox.mm

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,13 @@ - (void)dismiss
274274

275275
- (void)reload
276276
{
277-
[_actionDelegate reloadFromRedBoxController:self];
277+
if (_actionDelegate != nil) {
278+
[_actionDelegate reloadFromRedBoxController:self];
279+
} else {
280+
// In bridgeless mode `RCTRedBox` gets deallocated, we need to notify listeners anyway.
281+
RCTTriggerReloadCommandListeners(@"Redbox");
282+
[self dismiss];
283+
}
278284
}
279285

280286
- (void)showExtraDataViewController

packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTInstance.mm

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
#import <React/RCTLogBox.h>
2929
#import <React/RCTModuleData.h>
3030
#import <React/RCTPerformanceLogger.h>
31+
#import <React/RCTRedBox.h>
32+
#import <React/RCTReloadCommand.h>
3133
#import <React/RCTSurfacePresenter.h>
3234
#import <ReactCommon/RCTTurboModuleManager.h>
3335
#import <ReactCommon/RuntimeExecutor.h>
@@ -122,10 +124,17 @@ - (instancetype)initWithDelegate:(id<RCTInstanceDelegate>)delegate
122124
}];
123125
}
124126

125-
[[NSNotificationCenter defaultCenter] addObserver:self
126-
selector:@selector(_notifyEventDispatcherObserversOfEvent_DEPRECATED:)
127-
name:@"RCTNotifyEventDispatcherObserversOfEvent_DEPRECATED"
128-
object:nil];
127+
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
128+
129+
[defaultCenter addObserver:self
130+
selector:@selector(_notifyEventDispatcherObserversOfEvent_DEPRECATED:)
131+
name:@"RCTNotifyEventDispatcherObserversOfEvent_DEPRECATED"
132+
object:nil];
133+
134+
[defaultCenter addObserver:self
135+
selector:@selector(didReceiveReloadCommand)
136+
name:RCTTriggerReloadCommandNotification
137+
object:nil];
129138

130139
[self _start];
131140
}
@@ -389,6 +398,24 @@ - (void)_attachBridgelessAPIsToModule:(id<RCTTurboModule>)module
389398
}
390399
}
391400

401+
- (void)handleBundleLoadingError:(NSError *)error
402+
{
403+
if (!_valid) {
404+
return;
405+
}
406+
407+
RCTRedBox *redBox = [_turboModuleManager moduleForName:"RedBox"];
408+
409+
RCTExecuteOnMainQueue(^{
410+
[[NSNotificationCenter defaultCenter] postNotificationName:RCTJavaScriptDidFailToLoadNotification
411+
object:self
412+
userInfo:@{@"error" : error}];
413+
[redBox showErrorMessage:[error localizedDescription]];
414+
415+
RCTFatal(error);
416+
});
417+
}
418+
392419
- (void)_loadJSBundle:(NSURL *)sourceURL
393420
{
394421
#if RCT_DEV_MENU && __has_include(<React/RCTDevLoadingViewProtocol.h>)
@@ -420,8 +447,7 @@ - (void)_loadJSBundle:(NSURL *)sourceURL
420447
}
421448

422449
if (error) {
423-
// TODO(T91461138): Properly address bundle loading errors.
424-
RCTLogError(@"RCTInstance: Error while loading bundle: %@", error);
450+
[strongSelf handleBundleLoadingError:error];
425451
[strongSelf invalidate];
426452
return;
427453
}
@@ -490,4 +516,9 @@ - (void)_handleJSErrorMap:(facebook::react::MapBuffer)errorMap
490516
isFatal:errorMap.getBool(JSErrorHandlerKey::kIsFatal)];
491517
}
492518

519+
- (void)didReceiveReloadCommand
520+
{
521+
[self _loadJSBundle:[_bridgeModuleDecorator.bundleManager bundleURL]];
522+
}
523+
493524
@end

0 commit comments

Comments
 (0)