From f27acd81c85e3820ac03acbbc7fcc1971ad632ae Mon Sep 17 00:00:00 2001 From: Johnny Robeson Date: Wed, 10 Jun 2015 23:47:19 -0400 Subject: [PATCH] Simplify livereload injection by removing the script. This is one approach at solving #22 and #13. This PR removes the dynamically generated script, and loads it from the livereload server directly. It introduces a new configuration variable `options.liveReloadHost` and will take into account `options.ssl` to avoid mixed content warnings. --- index.js | 33 ++++++++------------------------- 1 file changed, 8 insertions(+), 25 deletions(-) diff --git a/index.js b/index.js index af45dc5..ac063a1 100644 --- a/index.js +++ b/index.js @@ -4,39 +4,22 @@ module.exports = { name: 'live-reload-middleware', contentFor: function(type) { - var liveReloadPort = process.env.EMBER_CLI_INJECT_LIVE_RELOAD_PORT; - var baseURL = process.env.EMBER_CLI_INJECT_LIVE_RELOAD_BASEURL; + var liveReloadScriptSrc = process.env.EMBER_CLI_INJECT_LIVE_RELOAD_SCRIPT_SRC; - if (liveReloadPort && type === 'head') { - return ''; + if (liveReloadScriptSrc && type === 'head') { + return ''; } }, - - dynamicScript: function(request) { - var liveReloadPort = process.env.EMBER_CLI_INJECT_LIVE_RELOAD_PORT; - - return "(function() {\n " + - "var src = (location.protocol || 'http:') + '//' + (location.hostname || 'localhost') + ':" + liveReloadPort + "/livereload.js?snipver=1';\n " + - "var script = document.createElement('script');\n " + - "script.type = 'text/javascript';\n " + - "script.src = src;\n " + - "document.getElementsByTagName('head')[0].appendChild(script);\n" + - "}());"; - }, - serverMiddleware: function(config) { - var self = this; - var app = config.app; var options = config.options; if (options.liveReload !== true) { return; } - process.env.EMBER_CLI_INJECT_LIVE_RELOAD_PORT = options.liveReloadPort; - process.env.EMBER_CLI_INJECT_LIVE_RELOAD_BASEURL = options.baseURL; // default is '/' + var liveReloadHost = options.liveReloadHost || options.host; + + var scheme = options.ssl ? 'https://' : 'http://'; + var src = [scheme, liveReloadHost, ':', options.liveReloadPort, '/livereload.js?snipver=1'].join(''); - app.use(options.baseURL + 'ember-cli-live-reload.js', function(request, response, next) { - response.contentType('text/javascript'); - response.send(self.dynamicScript()); - }); + process.env.EMBER_CLI_INJECT_LIVE_RELOAD_SCRIPT_SRC = src; } };