Skip to content

Commit 116392a

Browse files
Andrii.SasAndrii.Sas
Andrii.Sas
authored and
Andrii.Sas
committed
feat: link preload support (webpack-contrib#142)
1 parent 1ffc393 commit 116392a

File tree

5 files changed

+67
-1
lines changed

5 files changed

+67
-1
lines changed

src/index.js

+39-1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ class MiniCssExtractPlugin {
107107
filename: DEFAULT_FILENAME,
108108
moduleFilename: () => this.options.filename || DEFAULT_FILENAME,
109109
ignoreOrder: false,
110+
cssPreload: true,
110111
},
111112
options
112113
);
@@ -338,7 +339,7 @@ class MiniCssExtractPlugin {
338339
Template.indent([
339340
'var tag = existingLinkTags[i];',
340341
'var dataHref = tag.getAttribute("data-href") || tag.getAttribute("href");',
341-
'if(tag.rel === "stylesheet" && (dataHref === href || dataHref === fullhref)) return resolve();',
342+
'if((tag.rel === "stylesheet" || tag.rel === "preload") && (dataHref === href || dataHref === fullhref)) return resolve();',
342343
]),
343344
'}',
344345
'var existingStyleTags = document.getElementsByTagName("style");',
@@ -350,9 +351,46 @@ class MiniCssExtractPlugin {
350351
]),
351352
'}',
352353
'var linkTag = document.createElement("link");',
354+
this.options.cssPreload
355+
? Template.asString([
356+
'var isPreloadSupported = (function() {',
357+
Template.indent([
358+
'try { return linkTag.relList.supports("preload"); }',
359+
'catch(e) { return false; }',
360+
]),
361+
'}());',
362+
'if (isPreloadSupported) {',
363+
Template.indent([
364+
'linkTag.rel = "preload";',
365+
'linkTag.as = "style";',
366+
'linkTag.onload = function() {',
367+
Template.indent([
368+
'linkTag.rel = "stylesheet";',
369+
'linkTag.onload = null;',
370+
'resolve();',
371+
]),
372+
'};',
373+
]),
374+
'} else {',
375+
Template.indent([
376+
'linkTag.rel = "stylesheet";',
377+
'linkTag.type = "text/css";',
378+
'linkTag.media = "print";',
379+
'linkTag.onload = function() {',
380+
Template.indent([
381+
'linkTag.media = "all";',
382+
'linkTag.onload = null;',
383+
'resolve();',
384+
]),
385+
'};',
386+
]),
387+
'}',
388+
])
389+
: Template.asString([
353390
'linkTag.rel = "stylesheet";',
354391
'linkTag.type = "text/css";',
355392
'linkTag.onload = resolve;',
393+
]),
356394
'linkTag.onerror = function(event) {',
357395
Template.indent([
358396
'var request = event && event.target && event.target.src || fullhref;',

src/plugin-options.json

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
},
1414
"ignoreOrder": {
1515
"type": "boolean"
16+
},
17+
"cssPreload": {
18+
"type": "boolean"
1619
}
1720
}
1821
}

test/manual/index.html

+5
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@
5151
<p>An error should have appeared.</p>
5252
<p>Now if you turn the network back on and click it again, it should turn aqua.</p>
5353
</div>
54+
<div class="test lazy-link-preload">
55+
<p>Lazy CSS: Must be red.</p>
56+
<p><button class="lazy-link-preload-button">Pressing this button</button> displays an alert with check for link preload</p>
57+
<p>After click on alert should turn green.</p>
58+
</div>
5459
<div class="test preloaded-css1">
5560
<p>Preloaded CSS: Must be green.</p>
5661
<p><button class="preloaded-button1">Pressing this button</button> displays an alert and should turn red.</p>

test/manual/src/index.js

+17
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,23 @@ const makeButton = (className, fn, shouldDisable = true) => {
4545
makeButton('.lazy-button', () => import('./lazy.js'));
4646
makeButton('.lazy-button2', () => import('./lazy2.css'));
4747

48+
makeButton('.lazy-link-preload-button', () => {
49+
const promise = import('./lazy-link-preload.css');
50+
const linkElement = Array.from(document.head.children)
51+
.slice(-2)
52+
.find((el) => {
53+
return el.tagName === 'LINK';
54+
});
55+
56+
alert(
57+
`Is css preloaded by <link rel="preload"/>? - ${
58+
linkElement.getAttribute('rel') === 'preload' ? 'yes' : 'no'
59+
}`
60+
);
61+
62+
return promise;
63+
});
64+
4865
makeButton('.preloaded-button1', () =>
4966
import(/* webpackChunkName: "preloaded1" */ './preloaded1')
5067
);

test/manual/src/lazy-link-preload.css

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.lazy-link-preload {
2+
background: lightgreen;
3+
}

0 commit comments

Comments
 (0)