Skip to content

Commit

Permalink
fix: skip webpackPrefetch if preloaded
Browse files Browse the repository at this point in the history
  • Loading branch information
FogelAI committed Jul 13, 2023
1 parent d14f62c commit b785fd8
Showing 1 changed file with 48 additions and 20 deletions.
68 changes: 48 additions & 20 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1103,27 +1103,55 @@ class MiniCssExtractPlugin {
hasCssMatcher === true ? "true" : hasCssMatcher("chunkId")
}) {`,
Template.indent([
"installedCssChunks[chunkId] = null;",
linkPrefetch.call(
Template.asString([
"var link = document.createElement('link');",
crossOriginLoading
? `link.crossOrigin = ${JSON.stringify(
crossOriginLoading
)};`
: "",
`if (${RuntimeGlobals.scriptNonce}) {`,
Template.indent(
`link.setAttribute("nonce", ${RuntimeGlobals.scriptNonce});`
),
"}",
'link.rel = "prefetch";',
'link.as = "style";',
`link.href = ${RuntimeGlobals.publicPath} + ${RuntimeGlobals.require}.miniCssF(chunkId);`,
`var getLinkElements = function (rel, as) {`,
Template.indent([
`var links = document.getElementsByTagName("link");`,
`var loadedLinks = [];`,
`for (var i = 0; i < links.length; i++) {`,
Template.indent([
`if (`,
Template.indent([
`links[i].getAttribute("rel") === rel &&`,
`links[i].getAttribute("as") === as`,
]),
`) {`,
Template.indent([
`loadedLinks.push(links[i].getAttribute("href"));`,
]),
`}`,
]),
chunk
),
"document.head.appendChild(link);",
`}`,
`return loadedLinks;`,
]),
`};`,
"",
`var loadedPreloadLinkElements = getLinkElements("preload", "style");`,
`var chunkIdHref = ${RuntimeGlobals.publicPath} + ${RuntimeGlobals.require}.miniCssF(chunkId);`,
`if(!loadedPreloadLinkElements.includes(chunkIdHref)) {`,
Template.indent([
"installedCssChunks[chunkId] = null;",
linkPrefetch.call(
Template.asString([
"var link = document.createElement('link');",
crossOriginLoading
? `link.crossOrigin = ${JSON.stringify(
crossOriginLoading
)};`
: "",
`if (${RuntimeGlobals.scriptNonce}) {`,
Template.indent(
`link.setAttribute("nonce", ${RuntimeGlobals.scriptNonce});`
),
"}",
'link.rel = "prefetch";',
'link.as = "style";',
`link.href = ${RuntimeGlobals.publicPath} + ${RuntimeGlobals.require}.miniCssF(chunkId);`,
]),
chunk
),
"document.head.appendChild(link);",
]),
`}`,
]),
"}",
])};`
Expand Down

0 comments on commit b785fd8

Please sign in to comment.