Skip to content

Commit b785fd8

Browse files
committed
fix: skip webpackPrefetch if preloaded
1 parent d14f62c commit b785fd8

File tree

1 file changed

+48
-20
lines changed

1 file changed

+48
-20
lines changed

src/index.js

+48-20
Original file line numberDiff line numberDiff line change
@@ -1103,27 +1103,55 @@ class MiniCssExtractPlugin {
11031103
hasCssMatcher === true ? "true" : hasCssMatcher("chunkId")
11041104
}) {`,
11051105
Template.indent([
1106-
"installedCssChunks[chunkId] = null;",
1107-
linkPrefetch.call(
1108-
Template.asString([
1109-
"var link = document.createElement('link');",
1110-
crossOriginLoading
1111-
? `link.crossOrigin = ${JSON.stringify(
1112-
crossOriginLoading
1113-
)};`
1114-
: "",
1115-
`if (${RuntimeGlobals.scriptNonce}) {`,
1116-
Template.indent(
1117-
`link.setAttribute("nonce", ${RuntimeGlobals.scriptNonce});`
1118-
),
1119-
"}",
1120-
'link.rel = "prefetch";',
1121-
'link.as = "style";',
1122-
`link.href = ${RuntimeGlobals.publicPath} + ${RuntimeGlobals.require}.miniCssF(chunkId);`,
1106+
`var getLinkElements = function (rel, as) {`,
1107+
Template.indent([
1108+
`var links = document.getElementsByTagName("link");`,
1109+
`var loadedLinks = [];`,
1110+
`for (var i = 0; i < links.length; i++) {`,
1111+
Template.indent([
1112+
`if (`,
1113+
Template.indent([
1114+
`links[i].getAttribute("rel") === rel &&`,
1115+
`links[i].getAttribute("as") === as`,
1116+
]),
1117+
`) {`,
1118+
Template.indent([
1119+
`loadedLinks.push(links[i].getAttribute("href"));`,
1120+
]),
1121+
`}`,
11231122
]),
1124-
chunk
1125-
),
1126-
"document.head.appendChild(link);",
1123+
`}`,
1124+
`return loadedLinks;`,
1125+
]),
1126+
`};`,
1127+
"",
1128+
`var loadedPreloadLinkElements = getLinkElements("preload", "style");`,
1129+
`var chunkIdHref = ${RuntimeGlobals.publicPath} + ${RuntimeGlobals.require}.miniCssF(chunkId);`,
1130+
`if(!loadedPreloadLinkElements.includes(chunkIdHref)) {`,
1131+
Template.indent([
1132+
"installedCssChunks[chunkId] = null;",
1133+
linkPrefetch.call(
1134+
Template.asString([
1135+
"var link = document.createElement('link');",
1136+
crossOriginLoading
1137+
? `link.crossOrigin = ${JSON.stringify(
1138+
crossOriginLoading
1139+
)};`
1140+
: "",
1141+
`if (${RuntimeGlobals.scriptNonce}) {`,
1142+
Template.indent(
1143+
`link.setAttribute("nonce", ${RuntimeGlobals.scriptNonce});`
1144+
),
1145+
"}",
1146+
'link.rel = "prefetch";',
1147+
'link.as = "style";',
1148+
`link.href = ${RuntimeGlobals.publicPath} + ${RuntimeGlobals.require}.miniCssF(chunkId);`,
1149+
]),
1150+
chunk
1151+
),
1152+
"document.head.appendChild(link);",
1153+
]),
1154+
`}`,
11271155
]),
11281156
"}",
11291157
])};`

0 commit comments

Comments
 (0)