Skip to content

Commit bd891a0

Browse files
committed
refactor: use setInterval to check style is loaded in old webkit browser
1 parent 7b2b070 commit bd891a0

File tree

1 file changed

+32
-5
lines changed

1 file changed

+32
-5
lines changed

src/index.js

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -338,11 +338,22 @@ class MiniCssExtractPlugin {
338338
]),
339339
'}',
340340
'var linkTag = document.createElement("link");',
341-
'var isOldWebKit = Number(navigator.userAgent.replace(/.*AppleWebKit\\/(\\d+)..*/, "$1")) < 536;',
342341
'linkTag.rel = "stylesheet";',
343342
'linkTag.type = "text/css";',
344-
'if (!isOldWebKit) {',
345-
'linkTag.onload = resolve;',
343+
'linkTag.href = fullhref;',
344+
"// old webkit's would claim to have onload, but didn't really support it",
345+
'// https://github.com/kriszyp/xstyle/blob/master/core/load-css.js',
346+
'var webkitVersion = navigator.userAgent.match(/AppleWebKit\\/(\\d+\\.?\\d*)/);',
347+
'webkitVersion = webkitVersion && +webkitVersion[1];',
348+
'if(linkTag.onload === null && !(webkitVersion < 536)){',
349+
'// most browsers support this onload function now',
350+
'linkTag.onload = function(){',
351+
'// cleanup',
352+
'linkTag.onload = null;',
353+
'linkTag.onerror = null;',
354+
'resolve();',
355+
'};',
356+
'// always add the error handler, so we can notify of any errors',
346357
'linkTag.onerror = function(event) {',
347358
Template.indent([
348359
'var request = event && event.target && event.target.src || fullhref;',
@@ -351,11 +362,27 @@ class MiniCssExtractPlugin {
351362
'reject(err);',
352363
]),
353364
'};',
365+
'} else {',
366+
'var errorTimeout = 60000;',
367+
'var startTime = Date.now();',
368+
'var interval = setInterval(function(){',
369+
'if(linkTag.style){',
370+
'clearInterval(interval);',
371+
'resolve();',
372+
'}',
373+
'if(!linkTag.style && (Date.now() - startTime) > errorTimeout){',
374+
Template.indent([
375+
'var request = fullhref;',
376+
'var err = new Error("Loading CSS chunk " + chunkId + " timeout in old browser.\\n(" + request + ")");',
377+
'err.request = request;',
378+
'clearInterval(interval);',
379+
'reject(err);',
380+
]),
381+
'}',
382+
'}, 25);',
354383
'}',
355-
'linkTag.href = fullhref;',
356384
'var head = document.getElementsByTagName("head")[0];',
357385
'head.appendChild(linkTag);',
358-
'if (isOldWebKit) { resolve();}',
359386
]),
360387
'}).then(function() {',
361388
Template.indent(['installedCssChunks[chunkId] = 0;']),

0 commit comments

Comments
 (0)