Skip to content

Commit 4c4ebc0

Browse files
author
Robert Jackson
authored
Respect liveReloadPort even if same as ember server port (#113)
Respect liveReloadPort even if same as ember server port
2 parents ba3de60 + 9581f3b commit 4c4ebc0

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

lib/index.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,18 @@ module.exports = {
2222
}
2323

2424
let liveReloadPath = buildLiveReloadPath(options.liveReloadPrefix) || '/';
25-
let liveReloadPort;
2625
let path = '';
27-
if (options.liveReloadPort !== options.port) {
28-
liveReloadPort = options.liveReloadPort
29-
}
3026
if (options.isLatestEmber && options.liveReloadPrefix) {
3127
path = `&path=${options.liveReloadPrefix}/livereload`;
3228
}
3329
return `(function() {${liveReloadOptions ? "\n window.LiveReloadOptions = " + JSON.stringify(liveReloadOptions) + ";" : ''}
3430
var srcUrl = ${options.liveReloadJsUrl ? "'" + options.liveReloadJsUrl + "'" : null};
35-
var host= location.hostname || 'localhost';
36-
var liveReloadPort = ${liveReloadPort};
37-
var defaultPort = location.protocol === 'https:' ? 443 : 80;
38-
var port = liveReloadPort || location.port || defaultPort;
31+
var host = location.hostname || 'localhost';
32+
var useCustomPort = ${options.liveReloadPort !== options.port} || location.port !== ${options.liveReloadPort};
33+
var defaultPort = location.port || (location.protocol === 'https:' ? 443 : 80);
34+
var port = useCustomPort ? ${options.liveReloadPort} : defaultPort;
3935
var path = '${path}';
40-
var prefixURL = ${liveReloadPort ? "(location.protocol || 'http:') + '//' + host + ':' + " + liveReloadPort : "''"};
36+
var prefixURL = useCustomPort ? (location.protocol || 'http:') + '//' + host + ':' + ${options.liveReloadPort} : '';
4137
var src = srcUrl || prefixURL + '${liveReloadPath + 'livereload.js?port='}' + port + '&host=' + host + path;
4238
var script = document.createElement('script');
4339
script.type = 'text/javascript';

tests/inject-live-reload-test.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,15 @@ describe('dynamicScript returns right script when', hooks => {
9595
assert.strictEqual(actual, 'http://localhost:35729/_lr/livereload.js?port=35729&host=localhost&path=_lr/livereload');
9696
});
9797

98+
it('liveReloadPort and port are same, but both are different from location.port', assert => {
99+
options.liveReloadPort = '9999';
100+
options.port = '9999';
101+
let script = InjectLiveReload.dynamicScript(options);
102+
let actual = getScriptSrc(script);
103+
104+
assert.strictEqual(actual, 'http://localhost:9999/_lr/livereload.js?port=9999&host=localhost&path=_lr/livereload');
105+
});
106+
98107
it('liveReloadPrefix is provided', assert => {
99108
options.liveReloadPrefix = 'other-lr-path';
100109
let script = InjectLiveReload.dynamicScript(options);
@@ -161,12 +170,12 @@ describe('serverMiddleware', hooks => {
161170
response.on('end', () => {
162171
assert.equal(buf, `(function() {
163172
var srcUrl = null;
164-
var host= location.hostname || 'localhost';
165-
var liveReloadPort = undefined;
166-
var defaultPort = location.protocol === 'https:' ? 443 : 80;
167-
var port = liveReloadPort || location.port || defaultPort;
173+
var host = location.hostname || 'localhost';
174+
var useCustomPort = false || location.port !== 4200;
175+
var defaultPort = location.port || (location.protocol === 'https:' ? 443 : 80);
176+
var port = useCustomPort ? 4200 : defaultPort;
168177
var path = '';
169-
var prefixURL = '';
178+
var prefixURL = useCustomPort ? (location.protocol || 'http:') + '//' + host + ':' + 4200 : '';
170179
var src = srcUrl || prefixURL + '/livereload.js?port=' + port + '&host=' + host + path;
171180
var script = document.createElement('script');
172181
script.type = 'text/javascript';

0 commit comments

Comments
 (0)