Skip to content

Commit

Permalink
fix: fast refresh stops on needed bail outs (facebook#11105)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmmmwh authored and abhiisheek committed May 24, 2023
1 parent a7dfb9b commit e1a3c30
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions packages/react-dev-utils/webpackHotDevClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,18 @@ function canApplyUpdates() {
return module.hot.status() === 'idle';
}

function canAcceptErrors() {
// NOTE: This var is injected by Webpack's DefinePlugin, and is a boolean instead of string.
const hasReactRefresh = process.env.FAST_REFRESH;

const status = module.hot.status();
// React refresh can handle hot-reloading over errors.
// However, when hot-reload status is abort or fail,
// it indicates the current update cannot be applied safely,
// and thus we should bail out to a forced reload for consistency.
return hasReactRefresh && ["abort", "fail"].indexOf(status) === -1
}

// Attempt to update code on the fly, fall back to a hard reload.
function tryApplyUpdates(onHotUpdateSuccess) {
if (!module.hot) {
Expand All @@ -243,11 +255,13 @@ function tryApplyUpdates(onHotUpdateSuccess) {
}

function handleApplyUpdates(err, updatedModules) {
// NOTE: This var is injected by Webpack's DefinePlugin, and is a boolean instead of string.
const hasReactRefresh = process.env.FAST_REFRESH;
const wantsForcedReload = err || !updatedModules || hadRuntimeError;
// React refresh can handle hot-reloading over errors.
if (!hasReactRefresh && wantsForcedReload) {
const haveErrors = err || hadRuntimeError;
// When there is no error but updatedModules is unavailable,
// it indicates a critical failure in hot-reloading,
// e.g. server is not ready to serve new bundle,
// and hence we need to do a forced reload.
const needsForcedReload = !err && !updatedModules;
if ((haveErrors && !canAcceptErrors()) || needsForcedReload) {
window.location.reload();
return;
}
Expand Down

0 comments on commit e1a3c30

Please sign in to comment.