Skip to content

Commit c9e7294

Browse files
authored
Merge pull request #1 from TheRoadyBuddha/master
merge fix
2 parents 1ffc393 + d2c5202 commit c9e7294

File tree

4 files changed

+33
-11
lines changed

4 files changed

+33
-11
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,13 @@ module.exports = {
529529

530530
For long term caching use `filename: "[contenthash].css"`. Optionally add `[name]`.
531531

532+
#### rel="preload" support
533+
534+
On browsers that support `<link rel="preload" as="style">`, then CSS links will be preloaded by default.
535+
This is both to improve page load performance, and addresess Chrome Lighthouse SEO performance audits requiring the use of rel="preload" with asynchronous chunks.
536+
537+
-[Lighthouse article](https://developers.google.com/web/tools/lighthouse/audits/preload)
538+
532539
**webpack.config.js**
533540

534541
```js

src/index.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -321,10 +321,13 @@ class MiniCssExtractPlugin {
321321
}
322322
);
323323

324+
const supportsPreload =
325+
'(function() { try { return document.createElement("link").relList.supports("preload"); } catch(e) { return false; }}());';
324326
return Template.asString([
325327
source,
326328
'',
327329
`// ${pluginName} CSS loading`,
330+
`var supportsPreload = ${supportsPreload}`,
328331
`var cssChunks = ${JSON.stringify(chunkMap)};`,
329332
'if(installedCssChunks[chunkId]) promises.push(installedCssChunks[chunkId]);',
330333
'else if(installedCssChunks[chunkId] !== 0 && cssChunks[chunkId]) {',
@@ -338,7 +341,7 @@ class MiniCssExtractPlugin {
338341
Template.indent([
339342
'var tag = existingLinkTags[i];',
340343
'var dataHref = tag.getAttribute("data-href") || tag.getAttribute("href");',
341-
'if(tag.rel === "stylesheet" && (dataHref === href || dataHref === fullhref)) return resolve();',
344+
'if((tag.rel === "stylesheet" || tag.rel === "preload") && (dataHref === href || dataHref === fullhref)) return resolve();',
342345
]),
343346
'}',
344347
'var existingStyleTags = document.getElementsByTagName("style");',
@@ -350,8 +353,8 @@ class MiniCssExtractPlugin {
350353
]),
351354
'}',
352355
'var linkTag = document.createElement("link");',
353-
'linkTag.rel = "stylesheet";',
354-
'linkTag.type = "text/css";',
356+
'linkTag.rel = supportsPreload ? "preload": "stylesheet";',
357+
'supportsPreload ? linkTag.as = "style" : linkTag.type = "text/css";',
355358
'linkTag.onload = resolve;',
356359
'linkTag.onerror = function(event) {',
357360
Template.indent([
@@ -380,7 +383,18 @@ class MiniCssExtractPlugin {
380383
'head.appendChild(linkTag);',
381384
]),
382385
'}).then(function() {',
383-
Template.indent(['installedCssChunks[chunkId] = 0;']),
386+
Template.indent([
387+
'installedCssChunks[chunkId] = 0;',
388+
'if(supportsPreload) {',
389+
Template.indent([
390+
'var execLinkTag = document.createElement("link");',
391+
`execLinkTag.href = ${mainTemplate.requireFn}.p + ${linkHrefPath};`,
392+
'execLinkTag.rel = "stylesheet";',
393+
'execLinkTag.type = "text/css";',
394+
'document.body.appendChild(execLinkTag);',
395+
]),
396+
'}',
397+
]),
384398
'}));',
385399
]),
386400
'}',

test/HMR.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ describe('HMR', () => {
2727

2828
jest.spyOn(Date, 'now').mockImplementation(() => 1479427200000);
2929

30-
document.head.innerHTML = '<link rel="stylesheet" href="/dist/main.css" />';
30+
document.head.innerHTML =
31+
'<link rel="preload" as="style" href="/dist/main.css" />';
3132
document.body.innerHTML = '<script src="/dist/main.js"></script>';
3233
});
3334

test/__snapshots__/HMR.test.js.snap

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
exports[`HMR should handle error event 1`] = `"[HMR] css reload %s"`;
44

5-
exports[`HMR should handle error event 2`] = `"<link rel=\\"stylesheet\\" href=\\"/dist/main.css\\"><link rel=\\"stylesheet\\" href=\\"http://localhost/dist/main.css?1479427200000\\">"`;
5+
exports[`HMR should handle error event 2`] = `"<link rel=\\"preload\\" as=\\"style\\" href=\\"/dist/main.css\\"><link rel=\\"preload\\" as=\\"style\\" href=\\"http://localhost/dist/main.css?1479427200000\\">"`;
66
77
exports[`HMR should reloads with # link href 1`] = `"[HMR] css reload %s"`;
88
@@ -18,22 +18,22 @@ exports[`HMR should reloads with link without href 2`] = `"<link rel=\\"styleshe
1818
1919
exports[`HMR should reloads with locals 1`] = `"[HMR] Detected local css modules. Reload all css"`;
2020
21-
exports[`HMR should reloads with locals 2`] = `"<link rel=\\"stylesheet\\" href=\\"/dist/main.css\\"><link rel=\\"stylesheet\\" href=\\"http://localhost/dist/main.css?1479427200000\\">"`;
21+
exports[`HMR should reloads with locals 2`] = `"<link rel=\\"preload\\" as=\\"style\\" href=\\"/dist/main.css\\"><link rel=\\"preload\\" as=\\"style\\" href=\\"http://localhost/dist/main.css?1479427200000\\">"`;
2222
2323
exports[`HMR should reloads with non http/https link href 1`] = `"[HMR] css reload %s"`;
2424
2525
exports[`HMR should reloads with non http/https link href 2`] = `"<link rel=\\"stylesheet\\" href=\\"/dist/main.css\\"><link rel=\\"stylesheet\\" href=\\"http://localhost/dist/main.css?1479427200000\\"><link rel=\\"shortcut icon\\" href=\\"data:;base64,=\\">"`;
2626
2727
exports[`HMR should reloads with reloadAll option 1`] = `"[HMR] Reload all css"`;
2828
29-
exports[`HMR should reloads with reloadAll option 2`] = `"<link rel=\\"stylesheet\\" href=\\"/dist/main.css\\"><link rel=\\"stylesheet\\" href=\\"http://localhost/dist/main.css?1479427200000\\">"`;
29+
exports[`HMR should reloads with reloadAll option 2`] = `"<link rel=\\"preload\\" as=\\"style\\" href=\\"/dist/main.css\\"><link rel=\\"preload\\" as=\\"style\\" href=\\"http://localhost/dist/main.css?1479427200000\\">"`;
3030
3131
exports[`HMR should works 1`] = `"[HMR] css reload %s"`;
3232
33-
exports[`HMR should works 2`] = `"<link rel=\\"stylesheet\\" href=\\"/dist/main.css\\"><link rel=\\"stylesheet\\" href=\\"http://localhost/dist/main.css?1479427200000\\">"`;
33+
exports[`HMR should works 2`] = `"<link rel=\\"preload\\" as=\\"style\\" href=\\"/dist/main.css\\"><link rel=\\"preload\\" as=\\"style\\" href=\\"http://localhost/dist/main.css?1479427200000\\">"`;
3434
3535
exports[`HMR should works with multiple updates 1`] = `"[HMR] css reload %s"`;
3636
37-
exports[`HMR should works with multiple updates 2`] = `"<link rel=\\"stylesheet\\" href=\\"/dist/main.css\\"><link rel=\\"stylesheet\\" href=\\"http://localhost/dist/main.css?1479427200000\\">"`;
37+
exports[`HMR should works with multiple updates 2`] = `"<link rel=\\"preload\\" as=\\"style\\" href=\\"/dist/main.css\\"><link rel=\\"preload\\" as=\\"style\\" href=\\"http://localhost/dist/main.css?1479427200000\\">"`;
3838
39-
exports[`HMR should works with multiple updates 3`] = `"<link rel=\\"stylesheet\\" href=\\"http://localhost/dist/main.css?1479427200000\\"><link rel=\\"stylesheet\\" href=\\"http://localhost/dist/main.css?1479427200001\\">"`;
39+
exports[`HMR should works with multiple updates 3`] = `"<link rel=\\"preload\\" as=\\"style\\" href=\\"http://localhost/dist/main.css?1479427200000\\"><link rel=\\"preload\\" as=\\"style\\" href=\\"http://localhost/dist/main.css?1479427200001\\">"`;

0 commit comments

Comments
 (0)