Skip to content

Commit 31f3836

Browse files
author
Wietse de Vries
committed
Added preload support
1 parent af9a937 commit 31f3836

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

src/index.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,10 +364,19 @@ class MiniCssExtractPlugin {
364364
}
365365
);
366366

367+
const supportsPreload = `(function() {
368+
try {
369+
return document.createElement("link").relList.supports("preload");
370+
} catch(e) {
371+
return false;
372+
}
373+
}());`;
374+
367375
return Template.asString([
368376
source,
369377
'',
370378
`// ${pluginName} CSS loading`,
379+
`var supportsPreload = ${supportsPreload}`
371380
`var cssChunks = ${JSON.stringify(chunkMap)};`,
372381
'if(installedCssChunks[chunkId]) promises.push(installedCssChunks[chunkId]);',
373382
'else if(installedCssChunks[chunkId] !== 0 && cssChunks[chunkId]) {',
@@ -388,13 +397,15 @@ class MiniCssExtractPlugin {
388397
'for(var i = 0; i < existingStyleTags.length; i++) {',
389398
Template.indent([
390399
'var tag = existingStyleTags[i];',
400+
'if((tag.rel === "stylesheet" || tag.rel === "preload") && (dataHref === href || dataHref === fullhref)) return resolve();',
391401
'var dataHref = tag.getAttribute("data-href");',
392402
'if(dataHref === href || dataHref === fullhref) return resolve();',
393403
]),
394404
'}',
395405
'var linkTag = document.createElement("link");',
396-
'linkTag.rel = "stylesheet";',
406+
'linkTag.rel = supportsPreload ? "preload": "stylesheet";',
397407
'linkTag.type = "text/css";',
408+
'supportsPreload ? linkTag.as = "style" : linkTag.type = "text/css";',
398409
'linkTag.onload = resolve;',
399410
'linkTag.onerror = function(event) {',
400411
Template.indent([
@@ -423,7 +434,20 @@ class MiniCssExtractPlugin {
423434
'head.appendChild(linkTag);',
424435
]),
425436
'}).then(function() {',
426-
Template.indent(['installedCssChunks[chunkId] = 0;']),
437+
Template.indent([
438+
'installedCssChunks[chunkId] = 0;',
439+
'if(supportsPreload) {',
440+
Template.indent([
441+
'var execLinkTag = document.createElement("link");',
442+
`execLinkTag.href = ${
443+
mainTemplate.requireFn
444+
}.p + ${linkHrefPath};`,
445+
'execLinkTag.rel = "stylesheet";',
446+
'execLinkTag.type = "text/css";',
447+
'document.body.appendChild(execLinkTag);',
448+
]),
449+
'}',
450+
]),
427451
'}));',
428452
]),
429453
'}',

0 commit comments

Comments
 (0)