Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 12 additions & 0 deletions client/refreshUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ function shouldInvalidateReactRefreshBoundary(prevExports, nextExports) {
}

var enqueueUpdate = createDebounceUpdate();

function executeRuntime(
moduleExports,
moduleId,
Expand Down Expand Up @@ -245,6 +246,13 @@ function executeRuntime(
* @returns {void}
*/
function hotErrorHandler(error) {
if (
__reload_on_runtime_errors__ &&
isUnrecoverableRuntimeError(error)
) {
location.reload();
}

if (typeof refreshOverlay !== 'undefined' && refreshOverlay) {
refreshOverlay.handleRuntimeError(error);
}
Expand Down Expand Up @@ -287,6 +295,10 @@ function executeRuntime(
}
}

function isUnrecoverableRuntimeError(error) {
return error.message.startsWith('RuntimeError: factory is undefined');
}

module.exports = Object.freeze({
enqueueUpdate: enqueueUpdate,
executeRuntime: executeRuntime,
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class ReactRefreshRspackPlugin {
compiler.options.output.library,
),
),
__reload_on_runtime_errors__: this.options.reloadOnRuntimeErrors,
};
const providedModules: Record<string, string> = {
__react_refresh_utils__: refreshUtilsPath,
Expand Down
6 changes: 6 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<PluginOptions> {
Expand Down Expand Up @@ -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;
}