Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Webpack provide a global error hook when a chunk load fails?

After a migration from RequireJS to Webpack 4, I was looking for an equivalent to require.onError to capture runtime chunk load errors, but I couldn't find any options to add a global error handler.

I know that's possible to capture errors when using async import calls, like this one:

import('module/path')
.catch(function() {
    // error handling code
});

But, is there a Webpack option to add a generic chunk load error catch? so I can to provide further troubleshooting options to users (like a refresh the page prompt).

My app does heavy usage of async/lazy load and adding the error management code inline in every import call is not an option, since I need a fallback to make sure that there will be an error handler in case a dev forgets to add error management.

like image 510
jpgc Avatar asked Apr 22 '19 21:04

jpgc


People also ask

How do I fix a webpack error?

To solve the "Cannot find module 'webpack'" error, make sure to install webpack globally by running the npm i -g webpack command and create a symbolic link from the globally-installed package to node_modules by running the npm link webpack command. Copied! Once you run the two commands, the error should be resolved.

How does chunking work webpack?

Webpack injects some code into main. js which takes care of lazy loading async chunks and stops from loading same chunks again and again. When a route changes, React router calls a Webpack function to load a chunk file and Webpack after done loading runs it, which chunk then would internally ask React to do something.

What is runtime chunk in webpack?

Each of the runtime files contains code that enables loading of your chunks. If you open any of those runtime files, you will see code that loads your chunks via Jsonp. Since you have asked webpack to split chunks, you are now free to load any chunk any time.

What is a chunk load error?

To recap, the Chunk Load Error occurs when the browser encounters an error in fetching some JavaScript files, which were dynamically imported.

What happens when you bundle your source code in Webpack?

When webpack bundles your source code, it can become difficult to track down errors and warnings to their original location. For example, if you bundle three source files ( a.js, b.js, and c.js) into one bundle ( bundle.js) and one of the source files contains an error, the stack trace will simply point to bundle.js.

What is <chunkloaderror> in Webpack?

ChunkLoadError: Loading chunk * failed. Uncaught SyntaxError: Unexpected token ‘<’ ChunkLoadError: Loading chunk <random text> failed. If you are using Vue/Angular/React and other modularizable UI frameworks with webpack, you will eventually encounter this error at a certain point in time.

What is a chunk load error in JavaScript?

The multiple JavaScript files used are also known as chunks, and hence the name of the error. What Causes the Chunk Load Error? To recap, the Chunk Load Error occurs when the browser encounters an error in fetching some JavaScript files, which were dynamically imported. There are a couple reasons you may have encountered this error:

Is Webpack really that hard to configure?

I would say that this was quite a journey. I knew that Webpack was not easy to configure: there are many parts with many options, there’s npm hell, and they change with new releases. No wonder it can easily become a troublesome task to debug when something does not go as you expected (that is, not as it is in the docs).


1 Answers

Encountered same problem and explored webpack 4, but couldn't find anything similar to require.onError in webpack for dynamic imports. I have written a babel plugin that will attach catch to every dynamic import and will take error handler function definition in babel plugin options.

As dynamic imports are promises so chaining promises and attaching catch will attach custom error handler, without causing existing code to break.

Here is the babel plugin, https://www.npmjs.com/package/babel-plugin-dynamic-import-override

P.S. I didn't want to use window.onerror and wanted specific error handler.

like image 192
edify17 Avatar answered Oct 11 '22 07:10

edify17