Skip to content

Commit 0ce2460

Browse files
fix: do not try and HMR data urls (#165)
1 parent 00e329d commit 0ce2460

File tree

1 file changed

+66
-10
lines changed

1 file changed

+66
-10
lines changed

src/hmr/hotModuleReplacement.js

+66-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
/* global document, window */
2-
/* eslint func-names: 0 */
3-
/* eslint no-var: 0 */
4-
/* eslint vars-on-top: 0 */
5-
/* eslint prefer-arrow-func: 0 */
6-
/* eslint prefer-rest-params: 0 */
7-
/* eslint prefer-arrow-callback: 0 */
8-
/* eslint prefer-template: 0 */
2+
/*
3+
eslint-disable
4+
func-names,
5+
no-var,
6+
vars-on-top,
7+
prefer-arrow-func,
8+
prefer-rest-params,
9+
prefer-arrow-callback,
10+
prefer-template,
11+
prefer-destructuring,
12+
no-param-reassign,
13+
no-console
14+
*/
915

1016
var normalizeUrl = require('normalize-url');
1117

@@ -56,16 +62,21 @@ function getCurrentScriptUrl(moduleId) {
5662
if (!src) {
5763
return null;
5864
}
65+
5966
var splitResult = src.split(/([^\\/]+)\.js$/);
6067
var filename = splitResult && splitResult[1];
68+
6169
if (!filename) {
6270
return [src.replace('.js', '.css')];
6371
}
72+
6473
if (!fileMap) {
6574
return [src.replace('.js', '.css')];
6675
}
76+
6777
return fileMap.split(',').map(function(mapRule) {
6878
var reg = new RegExp(filename + '\\.js$', 'g');
79+
6980
return normalizeUrl(
7081
src.replace(reg, mapRule.replace(/{fileName}/g, filename) + '.css'),
7182
{ stripWWW: false }
@@ -78,14 +89,23 @@ function updateCss(el, url) {
7889
if (!url) {
7990
url = el.href.split('?')[0];
8091
}
92+
93+
if (!isUrlRequest(url)) {
94+
return;
95+
}
96+
8197
if (el.isLoaded === false) {
8298
// We seem to be about to replace a css link that hasn't loaded yet.
8399
// We're probably changing the same file more than once.
84100
return;
85101
}
86-
if (!url || !(url.indexOf('.css') > -1)) return;
102+
103+
if (!url || !(url.indexOf('.css') > -1)) {
104+
return;
105+
}
87106

88107
el.visited = true;
108+
89109
var newEl = el.cloneNode(); // eslint-disable-line vars-on-top
90110

91111
newEl.isLoaded = false;
@@ -101,18 +121,22 @@ function updateCss(el, url) {
101121
});
102122

103123
newEl.href = url + '?' + Date.now();
124+
104125
el.parentNode.appendChild(newEl);
105126
}
106127

107128
function getReloadUrl(href, src) {
108129
var ret;
130+
109131
href = normalizeUrl(href, { stripWWW: false });
132+
110133
// eslint-disable-next-line array-callback-return
111134
src.some(function(url) {
112135
if (href.indexOf(src) > -1) {
113136
ret = url;
114137
}
115138
});
139+
116140
return ret;
117141
}
118142

@@ -123,7 +147,13 @@ function reloadStyle(src) {
123147
forEach.call(elements, function(el) {
124148
var url = getReloadUrl(el.href, src);
125149

126-
if (el.visited === true) return;
150+
if (!isUrlRequest(url)) {
151+
return;
152+
}
153+
154+
if (el.visited === true) {
155+
return;
156+
}
127157

128158
if (url) {
129159
updateCss(el, url);
@@ -136,12 +166,37 @@ function reloadStyle(src) {
136166

137167
function reloadAll() {
138168
var elements = document.querySelectorAll('link');
169+
139170
forEach.call(elements, function(el) {
140-
if (el.visited === true) return;
171+
if (el.visited === true) {
172+
return;
173+
}
174+
141175
updateCss(el);
142176
});
143177
}
144178

179+
function isUrlRequest(url) {
180+
// An URL is not an request if
181+
182+
// 1. It's an absolute url
183+
if (/^[a-z][a-z0-9+.-]*:/i.test(url)) {
184+
return false;
185+
}
186+
187+
// 2. It's a protocol-relative
188+
if (/^\/\//.test(url)) {
189+
return false;
190+
}
191+
192+
// 3. Its a `#` link
193+
if (/^#/.test(url)) {
194+
return false;
195+
}
196+
197+
return true;
198+
}
199+
145200
module.exports = function(moduleId, options) {
146201
if (noDocument) {
147202
console.log('no window.document found, will not HMR CSS');
@@ -153,6 +208,7 @@ module.exports = function(moduleId, options) {
153208

154209
function update() {
155210
var src = getScriptSrc(options.filename);
211+
156212
var reloaded = reloadStyle(src);
157213

158214
if (options.locals) {

0 commit comments

Comments
 (0)