diff --git a/lib/index.js b/lib/index.js index f28830e..19a405f 100644 --- a/lib/index.js +++ b/lib/index.js @@ -29,7 +29,7 @@ module.exports = { return `(function() {${liveReloadOptions ? "\n window.LiveReloadOptions = " + JSON.stringify(liveReloadOptions) + ";" : ''} var srcUrl = ${options.liveReloadJsUrl ? "'" + options.liveReloadJsUrl + "'" : null}; var host = location.hostname || 'localhost'; - var useCustomPort = ${options.liveReloadPort !== options.port} || location.port !== ${options.liveReloadPort}; + var useCustomPort = ${options.liveReloadPort !== options.port} || (location.port !== '' && location.port !== ${options.liveReloadPort}); var defaultPort = location.port || (location.protocol === 'https:' ? 443 : 80); var port = useCustomPort ? ${options.liveReloadPort} : defaultPort; var path = '${path}'; diff --git a/tests/inject-live-reload-test.js b/tests/inject-live-reload-test.js index 22568e7..3728180 100644 --- a/tests/inject-live-reload-test.js +++ b/tests/inject-live-reload-test.js @@ -135,6 +135,25 @@ describe('dynamicScript returns right script when', hooks => { assert.strictEqual(actual, '/_lr/livereload.js?port=4200&host=localhost'); }); + + it('sets the proper port for http when served via domain', assert => { + location.port = ''; + + let script = InjectLiveReload.dynamicScript(options); + let actual = getScriptSrc(script); + + assert.strictEqual(actual, '/_lr/livereload.js?port=80&host=localhost&path=_lr/livereload'); + }); + + it('sets the proper port for http when served via domain', assert => { + location.port = ''; + location.protocol = 'https:' + + let script = InjectLiveReload.dynamicScript(options); + let actual = getScriptSrc(script); + + assert.strictEqual(actual, '/_lr/livereload.js?port=443&host=localhost&path=_lr/livereload'); + }); }); describe('serverMiddleware', hooks => { @@ -171,7 +190,7 @@ describe('serverMiddleware', hooks => { assert.equal(buf, `(function() { var srcUrl = null; var host = location.hostname || 'localhost'; - var useCustomPort = false || location.port !== 4200; + var useCustomPort = false || (location.port !== '' && location.port !== 4200); var defaultPort = location.port || (location.protocol === 'https:' ? 443 : 80); var port = useCustomPort ? 4200 : defaultPort; var path = '';