Skip to content

Commit

Permalink
fix: hmr runtime on preloaded stylesheet link (#686)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait authored Jan 27, 2021
1 parent a451223 commit 05e2951
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/hmr/hotModuleReplacement.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,19 @@ function updateCss(el, url) {
newEl.isLoaded = false;

newEl.addEventListener('load', () => {
if (newEl.isLoaded) {
return;
}

newEl.isLoaded = true;
el.parentNode.removeChild(el);
});

newEl.addEventListener('error', () => {
if (newEl.isLoaded) {
return;
}

newEl.isLoaded = true;
el.parentNode.removeChild(el);
});
Expand Down
25 changes: 25 additions & 0 deletions test/HMR.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,4 +306,29 @@ describe('HMR', () => {
done();
}, 100);
});

it('should not remove old link when new link is loaded twice', (done) => {
const link = document.createElement('link');

link.innerHTML = '<link rel="preload stylesheet" href="./dist/main.css" />';
document.head.appendChild(link);
document.head.removeChild = jest.fn();

const update = hotModuleReplacement('./dist/main.css', {});

update();

setTimeout(() => {
const links = Array.prototype.slice.call(
document.querySelectorAll('link')
);

links[1].dispatchEvent(getLoadEvent());
links[1].dispatchEvent(getLoadEvent());

expect(document.head.removeChild).toHaveBeenCalledTimes(1);

done();
}, 100);
});
});
8 changes: 8 additions & 0 deletions test/cases/hmr/expected/webpack-4/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -987,11 +987,19 @@ function updateCss(el, url) {
newEl.isLoaded = false;

newEl.addEventListener('load', () => {
if (newEl.isLoaded) {
return;
}

newEl.isLoaded = true;
el.parentNode.removeChild(el);
});

newEl.addEventListener('error', () => {
if (newEl.isLoaded) {
return;
}

newEl.isLoaded = true;
el.parentNode.removeChild(el);
});
Expand Down
8 changes: 8 additions & 0 deletions test/cases/hmr/expected/webpack-5/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,19 @@ function updateCss(el, url) {
newEl.isLoaded = false;

newEl.addEventListener('load', () => {
if (newEl.isLoaded) {
return;
}

newEl.isLoaded = true;
el.parentNode.removeChild(el);
});

newEl.addEventListener('error', () => {
if (newEl.isLoaded) {
return;
}

newEl.isLoaded = true;
el.parentNode.removeChild(el);
});
Expand Down

0 comments on commit 05e2951

Please sign in to comment.