Skip to content

Commit dfb2e73

Browse files
authored
fix: Quick fix to handle multiple hot updates for the same file
1 parent 6d68cef commit dfb2e73

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

hotModuleReplacement.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
var currentlyProcessing = {};
12
module.exports = function(publicPath, outputFilename) {
23
if (document) {
34
var newHref = publicPath.match(/https?:/g) ? new URL(outputFilename, publicPath) : new URL(publicPath + outputFilename, window.location);
45
var links = document.getElementsByTagName('link');
56

6-
//update the stylesheet corresponding to `outputFilename`
7+
//update the stylesheet corresponding to `outputFilename`
78
for (var i = 0; i < links.length; i++) {
89
if (links[i].href) {
910
var oldChunk = new URL(links[i].href);
@@ -19,15 +20,24 @@ module.exports = function(publicPath, outputFilename) {
1920
link.charset = 'utf-8'
2021
link.type = 'text/css'
2122
link.rel = 'stylesheet'
22-
23+
24+
// TODO: Look into using __webpack_hash__
25+
if (!currentlyProcessing[oldChunk.pathname]) {
26+
currentlyProcessing[oldChunk.pathname] = [oldSheet];
27+
} else {
28+
currentlyProcessing[oldChunk.pathname].push(link);
29+
}
2330
head.insertBefore(link, oldSheet.nextSibling)
2431

2532
// remove the old sheet only after the old one loads so it's seamless
2633
// we gotta do it this way since link.onload basically doesn't work
2734
var img = document.createElement('img')
2835
img.onerror = function() {
29-
oldSheet.remove()
30-
console.log('[HMR]', 'Reload css: ', url);
36+
if (currentlyProcessing[oldChunk.pathname]) {
37+
currentlyProcessing[oldChunk.pathname].forEach(old => old.remove());
38+
currentlyProcessing[oldChunk.pathname] = null;
39+
console.log('[HMR]', 'Reload css: ', url);
40+
}
3141
}
3242
img.src = url
3343
break;

0 commit comments

Comments
 (0)