Skip to content

Commit a40963c

Browse files
committed
feat: add reloadOnRuntimeErrors to config reload when apply hot-update errors
1 parent 0579649 commit a40963c

3 files changed

Lines changed: 22 additions & 0 deletions

File tree

client/refreshUtils.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ function shouldInvalidateReactRefreshBoundary(prevExports, nextExports) {
209209
}
210210

211211
var enqueueUpdate = createDebounceUpdate();
212+
212213
function executeRuntime(
213214
moduleExports,
214215
moduleId,
@@ -245,8 +246,18 @@ function executeRuntime(
245246
* @returns {void}
246247
*/
247248
function hotErrorHandler(error) {
249+
if (
250+
__reload_on_runtime_errors__ &&
251+
isUnrecoverableRuntimeError(error)
252+
) {
253+
location.reload();
254+
}
255+
248256
if (typeof refreshOverlay !== 'undefined' && refreshOverlay) {
249257
refreshOverlay.handleRuntimeError(error);
258+
} else {
259+
console.log('can afford it keep throw up');
260+
// throw error;
250261
}
251262

252263
if (typeof isTest !== 'undefined' && isTest) {
@@ -287,6 +298,10 @@ function executeRuntime(
287298
}
288299
}
289300

301+
function isUnrecoverableRuntimeError(error) {
302+
return error.message.startsWith('RuntimeError: factory is undefined');
303+
}
304+
290305
module.exports = Object.freeze({
291306
enqueueUpdate: enqueueUpdate,
292307
executeRuntime: executeRuntime,

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ class ReactRefreshRspackPlugin {
106106
compiler.options.output.library,
107107
),
108108
),
109+
__reload_on_runtime_errors__: this.options.reloadOnRuntimeErrors,
109110
};
110111
const providedModules: Record<string, string> = {
111112
__react_refresh_utils__: refreshUtilsPath,

src/options.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ export type PluginOptions = {
7979
* @default true
8080
*/
8181
injectEntry?: boolean;
82+
/**
83+
* Whether to reload the page on runtime errors. E.g: undefined module factory
84+
* @default false
85+
*/
86+
reloadOnRuntimeErrors?: boolean;
8287
};
8388

8489
export interface NormalizedPluginOptions extends Required<PluginOptions> {
@@ -128,6 +133,7 @@ export function normalizeOptions(
128133
d(options, 'forceEnable', false);
129134
d(options, 'injectLoader', true);
130135
d(options, 'injectEntry', true);
136+
d(options, 'reloadOnRuntimeErrors', false);
131137
options.overlay = normalizeOverlay(options.overlay);
132138
return options as NormalizedPluginOptions;
133139
}

0 commit comments

Comments
 (0)