diff --git a/README.md b/README.md index 7d78e8e..88ca908 100644 --- a/README.md +++ b/README.md @@ -217,6 +217,21 @@ new ReactRefreshPlugin({ }); ``` +### reloadOnRuntimeErrors + +- Type: `boolean` +- Default: `false` + +Config the plugin whether trigger a full page reload when an unrecoverable runtime error is encountered. + +Currently, only module factory undefined error is considered as unrecoverable runtime error. + +```js +new ReactRefreshPlugin({ + reloadOnRuntimeErrors: true, +}); +``` + ## Credits Thanks to the [react-refresh-webpack-plugin](https://github.com/pmmmwh/react-refresh-webpack-plugin) created by [@pmmmwh](https://github.com/pmmmwh), which inspires implement this plugin. diff --git a/client/refreshUtils.js b/client/refreshUtils.js index b3aa8f3..ced967e 100644 --- a/client/refreshUtils.js +++ b/client/refreshUtils.js @@ -209,6 +209,7 @@ function shouldInvalidateReactRefreshBoundary(prevExports, nextExports) { } var enqueueUpdate = createDebounceUpdate(); + function executeRuntime( moduleExports, moduleId, @@ -246,6 +247,12 @@ function executeRuntime( */ function hotErrorHandler(error) { console.error(error); + if ( + __reload_on_runtime_errors__ && + isUnrecoverableRuntimeError(error) + ) { + location.reload(); + } if (typeof refreshOverlay !== 'undefined' && refreshOverlay) { refreshOverlay.handleRuntimeError(error); @@ -289,6 +296,10 @@ function executeRuntime( } } +function isUnrecoverableRuntimeError(error) { + return error.message.startsWith('RuntimeError: factory is undefined'); +} + module.exports = Object.freeze({ enqueueUpdate: enqueueUpdate, executeRuntime: executeRuntime, diff --git a/src/index.ts b/src/index.ts index 6c8a976..aebf279 100644 --- a/src/index.ts +++ b/src/index.ts @@ -106,6 +106,7 @@ class ReactRefreshRspackPlugin { compiler.options.output.library, ), ), + __reload_on_runtime_errors__: this.options.reloadOnRuntimeErrors, }; const providedModules: Record = { __react_refresh_utils__: refreshUtilsPath, diff --git a/src/options.ts b/src/options.ts index 729998e..96fe364 100644 --- a/src/options.ts +++ b/src/options.ts @@ -79,6 +79,11 @@ export type PluginOptions = { * @default true */ injectEntry?: boolean; + /** + * Whether to reload the page on runtime errors. E.g: undefined module factory + * @default false + */ + reloadOnRuntimeErrors?: boolean; }; export interface NormalizedPluginOptions extends Required { @@ -128,6 +133,7 @@ export function normalizeOptions( d(options, 'forceEnable', false); d(options, 'injectLoader', true); d(options, 'injectEntry', true); + d(options, 'reloadOnRuntimeErrors', false); options.overlay = normalizeOverlay(options.overlay); return options as NormalizedPluginOptions; }