From 79bde8ce025b6c4603afdba0172a9e27a5b399ee Mon Sep 17 00:00:00 2001 From: "Seasons.Lee" Date: Tue, 8 May 2018 09:53:07 +0800 Subject: [PATCH 1/3] feat: add css async load support to old webkit browser --- src/index.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/index.js b/src/index.js index 6aa5436f..cc878d1a 100644 --- a/src/index.js +++ b/src/index.js @@ -338,8 +338,10 @@ class MiniCssExtractPlugin { ]), '}', 'var linkTag = document.createElement("link");', + 'var isOldWebKit = Number(UA.replace(/.*AppleWebKit/(d+)..*/, "$1")) < 536;', 'linkTag.rel = "stylesheet";', 'linkTag.type = "text/css";', + 'if (!isOldWebKit) {', 'linkTag.onload = resolve;', 'linkTag.onerror = function(event) {', Template.indent([ @@ -349,9 +351,11 @@ class MiniCssExtractPlugin { 'reject(err);', ]), '};', + '}', 'linkTag.href = fullhref;', 'var head = document.getElementsByTagName("head")[0];', 'head.appendChild(linkTag);', + 'if (isOldWebKit) { resolve();}', ]), '}).then(function() {', Template.indent(['installedCssChunks[chunkId] = 0;']), From 7b2b070432601b739a16dc04e4b3ad10499eb15e Mon Sep 17 00:00:00 2001 From: "Seasons.Lee" Date: Tue, 8 May 2018 14:19:32 +0800 Subject: [PATCH 2/3] fix: change old browser regex defined --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index cc878d1a..16067d3e 100644 --- a/src/index.js +++ b/src/index.js @@ -338,7 +338,7 @@ class MiniCssExtractPlugin { ]), '}', 'var linkTag = document.createElement("link");', - 'var isOldWebKit = Number(UA.replace(/.*AppleWebKit/(d+)..*/, "$1")) < 536;', + 'var isOldWebKit = Number(navigator.userAgent.replace(/.*AppleWebKit\\/(\\d+)..*/, "$1")) < 536;', 'linkTag.rel = "stylesheet";', 'linkTag.type = "text/css";', 'if (!isOldWebKit) {', From bd891a0e9de7c203a52b62b09825e8c879e100db Mon Sep 17 00:00:00 2001 From: "Seasons.Lee" Date: Sat, 12 May 2018 19:00:41 +0800 Subject: [PATCH 3/3] refactor: use setInterval to check style is loaded in old webkit browser --- src/index.js | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/src/index.js b/src/index.js index 16067d3e..37c5f1be 100644 --- a/src/index.js +++ b/src/index.js @@ -338,11 +338,22 @@ class MiniCssExtractPlugin { ]), '}', 'var linkTag = document.createElement("link");', - 'var isOldWebKit = Number(navigator.userAgent.replace(/.*AppleWebKit\\/(\\d+)..*/, "$1")) < 536;', 'linkTag.rel = "stylesheet";', 'linkTag.type = "text/css";', - 'if (!isOldWebKit) {', - '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;', @@ -351,11 +362,27 @@ class MiniCssExtractPlugin { 'reject(err);', ]), '};', + '} 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);', '}', - 'linkTag.href = fullhref;', 'var head = document.getElementsByTagName("head")[0];', 'head.appendChild(linkTag);', - 'if (isOldWebKit) { resolve();}', ]), '}).then(function() {', Template.indent(['installedCssChunks[chunkId] = 0;']),