diff --git a/src/index.js b/src/index.js index 6aa5436f..37c5f1be 100644 --- a/src/index.js +++ b/src/index.js @@ -340,7 +340,20 @@ class MiniCssExtractPlugin { 'var linkTag = document.createElement("link");', 'linkTag.rel = "stylesheet";', 'linkTag.type = "text/css";', - 'linkTag.onload = resolve;', + 'linkTag.href = fullhref;', + "// old webkit's would claim to have onload, but didn't really support it", + '// https://github.com/kriszyp/xstyle/blob/master/core/load-css.js', + 'var webkitVersion = navigator.userAgent.match(/AppleWebKit\\/(\\d+\\.?\\d*)/);', + 'webkitVersion = webkitVersion && +webkitVersion[1];', + 'if(linkTag.onload === null && !(webkitVersion < 536)){', + '// most browsers support this onload function now', + 'linkTag.onload = function(){', + '// cleanup', + 'linkTag.onload = null;', + 'linkTag.onerror = null;', + 'resolve();', + '};', + '// always add the error handler, so we can notify of any errors', 'linkTag.onerror = function(event) {', Template.indent([ 'var request = event && event.target && event.target.src || fullhref;', @@ -349,7 +362,25 @@ class MiniCssExtractPlugin { 'reject(err);', ]), '};', - 'linkTag.href = fullhref;', + '} else {', + 'var errorTimeout = 60000;', + 'var startTime = Date.now();', + 'var interval = setInterval(function(){', + 'if(linkTag.style){', + 'clearInterval(interval);', + 'resolve();', + '}', + 'if(!linkTag.style && (Date.now() - startTime) > errorTimeout){', + Template.indent([ + 'var request = fullhref;', + 'var err = new Error("Loading CSS chunk " + chunkId + " timeout in old browser.\\n(" + request + ")");', + 'err.request = request;', + 'clearInterval(interval);', + 'reject(err);', + ]), + '}', + '}, 25);', + '}', 'var head = document.getElementsByTagName("head")[0];', 'head.appendChild(linkTag);', ]),