From 5e960e30eb3515c3dcdf31efee233aed327fda72 Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Wed, 24 Jul 2019 11:04:47 -0700 Subject: [PATCH] Don't show warnings unless we managed to connect Summary: On iOS we don't call `HMRClient.setup()` when Metro is off. So we don't bump into any odd cases. But on Android, we do call `HMRClient.setup()` even if Metro is off. As a result, we might show a warning about Metro not running to a native engineer who doesn't care (because they don't intend to work on JS). We could fix this on Android on the native side. And we probably should. But we can also strengthen it here. The idea is that we should only show warnings about disconnecting from Metro *if we ever managed to successfully connect in the first place*. Otherwise, we can assume that you didn't mean to connect. If the user is trying to determine the source of the problem, they can still do a full Refresh (on iOS this will show a message about needing Metro, on Android it would show a redbox). So this diff makes the disconnected behavior closer to how it worked before Fast Refresh. Reviewed By: sahrens Differential Revision: D16460439 fbshipit-source-id: bf962ff34c25d9734d9668dd583591acacb98253 --- Libraries/Utilities/HMRClient.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Libraries/Utilities/HMRClient.js b/Libraries/Utilities/HMRClient.js index 573af735eab..bc2a5800304 100644 --- a/Libraries/Utilities/HMRClient.js +++ b/Libraries/Utilities/HMRClient.js @@ -22,6 +22,7 @@ const pendingEntryPoints = []; let hmrClient = null; let hmrUnavailableReason: string | null = null; let currentCompileErrorMessage: string | null = null; +let didConnect: boolean = false; type LogLevel = | 'trace' @@ -169,6 +170,8 @@ Error: ${e.message}`; client.on('update-start', ({isInitialUpdate}) => { currentCompileErrorMessage = null; + didConnect = true; + if (client.isEnabled() && !isInitialUpdate) { LoadingView.showMessage('Refreshing...', 'refresh'); } @@ -229,8 +232,11 @@ function setHMRUnavailableReason(reason) { return; } hmrUnavailableReason = reason; - if (hmrClient.isEnabled()) { - // If HMR is currently enabled, show a warning. + + // We only want to show a warning if Fast Refresh is on *and* if we ever + // previously managed to connect successfully. We don't want to show + // the warning to native engineers who use cached bundles without Metro. + if (hmrClient.isEnabled() && didConnect) { console.warn(reason); // (Not using the `warning` module to prevent a Buck cycle.) }