Skip to content

Commit 89e7a0a

Browse files
fix: error when reloading async chunk (#633)
1 parent 19ec95b commit 89e7a0a

File tree

5 files changed

+41
-0
lines changed

5 files changed

+41
-0
lines changed

Diff for: src/hmr/hotModuleReplacement.js

+4
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ function getReloadUrl(href, src) {
143143
}
144144

145145
function reloadStyle(src) {
146+
if (!src) {
147+
return false;
148+
}
149+
146150
const elements = document.querySelectorAll('link');
147151
let loaded = false;
148152

Diff for: test/HMR.test.js

+25
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,31 @@ describe('HMR', () => {
259259
}, 100);
260260
});
261261

262+
it('should reloads with non-file script in the end of page', (done) => {
263+
document.body.appendChild(document.createElement('script'));
264+
265+
const update = hotModuleReplacement('./src/non_file_styles.css', {});
266+
267+
update();
268+
269+
setTimeout(() => {
270+
expect(console.log.mock.calls[0][0]).toMatchSnapshot();
271+
272+
const links = Array.prototype.slice.call(
273+
document.querySelectorAll('link')
274+
);
275+
276+
expect(links[0].visited).toBe(true);
277+
expect(document.head.innerHTML).toMatchSnapshot();
278+
279+
links[1].dispatchEvent(getLoadEvent());
280+
281+
expect(links[1].isLoaded).toBe(true);
282+
283+
done();
284+
}, 100);
285+
});
286+
262287
it('should handle error event', (done) => {
263288
const update = hotModuleReplacement('./src/style.css', {});
264289

Diff for: test/__snapshots__/HMR.test.js.snap

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ exports[`HMR should reloads with non http/https link href 1`] = `"[HMR] css relo
2424
2525
exports[`HMR should reloads with non http/https link href 2`] = `"<link rel=\\"stylesheet\\" href=\\"/dist/main.css\\"><link rel=\\"stylesheet\\" href=\\"http://localhost/dist/main.css?1479427200000\\"><link rel=\\"shortcut icon\\" href=\\"data:;base64,=\\">"`;
2626
27+
exports[`HMR should reloads with non-file script in the end of page 1`] = `"[HMR] Reload all css"`;
28+
29+
exports[`HMR should reloads with non-file script in the end of page 2`] = `"<link rel=\\"stylesheet\\" href=\\"/dist/main.css\\"><link rel=\\"stylesheet\\" href=\\"http://localhost/dist/main.css?1479427200000\\">"`;
30+
2731
exports[`HMR should work reload all css 1`] = `"[HMR] Reload all css"`;
2832
2933
exports[`HMR should work reload all css 2`] = `"<link rel=\\"stylesheet\\" href=\\"/dist/main.css\\"><link rel=\\"stylesheet\\" href=\\"http://localhost/dist/main.css?1479427200000\\">"`;

Diff for: test/cases/hmr/expected/webpack-4/main.js

+4
Original file line numberDiff line numberDiff line change
@@ -1022,6 +1022,10 @@ function getReloadUrl(href, src) {
10221022
}
10231023

10241024
function reloadStyle(src) {
1025+
if (!src) {
1026+
return false;
1027+
}
1028+
10251029
const elements = document.querySelectorAll('link');
10261030
let loaded = false;
10271031

Diff for: test/cases/hmr/expected/webpack-5/main.js

+4
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ function getReloadUrl(href, src) {
164164
}
165165

166166
function reloadStyle(src) {
167+
if (!src) {
168+
return false;
169+
}
170+
167171
const elements = document.querySelectorAll('link');
168172
let loaded = false;
169173

0 commit comments

Comments
 (0)