From 577599428d4de9d4338c7c536c7f0e1897e370a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kurt=20Inge=20Sma=CC=8Adal?= Date: Fri, 8 Jan 2021 14:03:55 +0100 Subject: [PATCH] Do not add style as body link if it already exists --- src/index.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index 62a515da..467b1155 100644 --- a/src/index.js +++ b/src/index.js @@ -330,6 +330,7 @@ class ExtractCssChunksPlugin { '', `// ${pluginName} CSS loading`, `var supportsPreload = ${supportsPreload}`, + `var linkExists = false`, `var cssChunks = ${JSON.stringify(chunkMap)};`, 'if(installedCssChunks[chunkId]) promises.push(installedCssChunks[chunkId]);', 'else if(installedCssChunks[chunkId] !== 0 && cssChunks[chunkId]) {', @@ -343,7 +344,13 @@ class ExtractCssChunksPlugin { Template.indent([ 'var tag = existingLinkTags[i];', 'var dataHref = tag.getAttribute("data-href") || tag.getAttribute("href");', - 'if((tag.rel === "stylesheet" || tag.rel === "preload") && (dataHref === href || dataHref === fullhref)) return resolve();', ]), + 'if((tag.rel === "stylesheet" || tag.rel === "preload") && (dataHref === href || dataHref === fullhref)) {', + Template.indent([ + 'linkExists = true', + 'return resolve();', + ]), + '}', + ]), '}', 'var existingStyleTags = document.getElementsByTagName("style");', 'for(var i = 0; i < existingStyleTags.length; i++) {', @@ -387,7 +394,7 @@ class ExtractCssChunksPlugin { '}).then(function() {', Template.indent([ 'installedCssChunks[chunkId] = 0;', - 'if(supportsPreload) {', + 'if(!linkExists && supportsPreload) {', Template.indent([ 'var execLinkTag = document.createElement("link");', `execLinkTag.href = ${mainTemplate.requireFn}.p + ${linkHrefPath};`,