Skip to content

Commit e86fa3b

Browse files
committed
avoid adding styles multiple times
1 parent e062cd2 commit e86fa3b

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

src/index.js

+32-2
Original file line numberDiff line numberDiff line change
@@ -197,13 +197,43 @@ class MiniCssExtractPlugin {
197197
Template.indent([
198198
'promises.push(installedCssChunks[chunkId] = new Promise(function(resolve, reject) {',
199199
Template.indent([
200+
201+
`var href = ${linkHrefPath};`,
202+
`var fullhref = ${mainTemplate.requireFn}.p + href;`,
203+
'var existingLinkTags = document.getElementsByTagName("link");',
204+
'for(var i = 0; i < existingLinkTags.length; i++) {',
205+
Template.indent([
206+
'var tag = existingLinkTags[i];',
207+
'var dataHref = tag.getAttribute("data-href") || tag.getAttribute("href");',
208+
'if(tag.rel === "stylesheet" && (dataHref === href || dataHref === fullhref)) return resolve();',
209+
]),
210+
'}',
211+
'var existingStyleTags = document.getElementsByTagName("style");',
212+
'for(var i = 0; i < existingStyleTags.length; i++) {',
213+
Template.indent([
214+
'var tag = existingStyleTags[i];',
215+
'var dataHref = tag.getAttribute("data-href");',
216+
'if(dataHref === href || dataHref === fullhref) return resolve();',
217+
]),
218+
'}',
200219
'var linkTag = document.createElement("link");',
201220
'linkTag.rel = "stylesheet";',
221+
'linkTag.type = "text/css";',
202222
'linkTag.onload = resolve;',
203-
'linkTag.onerror = reject;',
204-
`linkTag.href = ${mainTemplate.requireFn}.p + ${linkHrefPath};`,
223+
'linkTag.onerror = function(event) {',
224+
Template.indent([
225+
'var request = event && event.target && event.target.src || fullhref;',
226+
'var err = new Error("Loading CSS chunk " + chunkId + " failed.\\n(" + request + ")");',
227+
'err.request = request;',
228+
'reject(err);',
229+
]),
230+
'};',
231+
'linkTag.href = fullhref;',
205232
'var head = document.getElementsByTagName("head")[0];',
206233
'head.appendChild(linkTag);',
234+
]),
235+
'}).then(function() {',
236+
Template.indent([
207237
'installedCssChunks[chunkId] = 0;',
208238
]),
209239
'}));',

0 commit comments

Comments
 (0)