Option for async chunk loading with network error

Do you want to request a feature or report a bug?

Feature

What is the current behavior?

For code splitting with async chunk loading,
when the chunk fail to load, it will call the error callback.
There are no option to have a fallback url for the chunk

What is the expected behavior?

By default is load the chunk base on public path, etc http://cdn.example.com/chunk-1.js
if http://cdn.example.com/chunk-1.js fail to load (404 or timeout),
I can config and fallback path, like http://another-cdn.example.com
so that it will try to load http://another-cdn.example.com/chunk-1.js

If this is a feature request, what is motivation or use case for changing the behavior?

The use case is if the CDN network fail in some region, it can automatically fallback to another CDN or the web server to get the chunk.

Please mention other relevant information such as the browser version, Node.js version, webpack version and Operating System.

I am using webpack 2, node v6.9.4, macOS Sierra

Author: Fantashit

1 thought on “Option for async chunk loading with network error

  1. Try this:

    const cdns = [
    	"https://cdn1.example.com/",
    	"https://cdn2.example.com/",
    	"https://cdn3.example.com/",
    ];
    
    let currentCdn = 0;
    
    const original = __webpack_chunk_load__;
    __webpack_chunk_load__ = (id) => {
    	let n = cdns.length;
    	return (function tryCdn() {
    		if(--n === 0)
    			return original(id);
    		return original(id).catch(e => {
    			currentCdn = (currentCdn + 1) %% cdns.length;
    			__webpack_public_path__ = cdns[currentCdn];
    			return tryCdn();
    		})
    	}());
    }

Comments are closed.