Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use process.env.EMBER_CLI_INJECT_LIVE_RELOAD_HOST #5

Closed
wants to merge 2 commits into from

Conversation

pgasiorowski
Copy link

Make use of environmental variable EMBER_CLI_INJECT_LIVE_RELOAD_HOST if available

Started here: https://github.com/rwjblue/ember-cli-inject-live-reload/pull/4#issuecomment-57911657

@@ -7,7 +7,9 @@ module.exports = {
var liveReloadPort = process.env.EMBER_CLI_INJECT_LIVE_RELOAD_PORT;

if (liveReloadPort && type === 'head') {
return '<script src="http://localhost:' + liveReloadPort + '/livereload.js?snipver=1" type="text/javascript"></script>';
var liveReloadHost = process.env.EMBER_CLI_INJECT_LIVE_RELOAD_HOST || '//localhost';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we need the // either way?

@pgasiorowski
Copy link
Author

Updated PR. Hopefully this will do it

@pgasiorowski
Copy link
Author

I don't have much experience with node but wouldn't it be more convenient to somehow get the hostname from request's headers?

@rwjblue
Copy link
Member

rwjblue commented Oct 5, 2014

This file is rebuilt once for all clients, not for a single one. It is likely that some trickery could do exactly what you are suggesting, but it isn't obvious.

@@ -7,7 +7,9 @@ module.exports = {
var liveReloadPort = process.env.EMBER_CLI_INJECT_LIVE_RELOAD_PORT;

if (liveReloadPort && type === 'head') {
return '<script src="http://localhost:' + liveReloadPort + '/livereload.js?snipver=1" type="text/javascript"></script>';
var liveReloadHost = process.env.EMBER_CLI_INJECT_LIVE_RELOAD_HOST || 'localhost';
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stylistically, i'd put this after the var liveReloadPort in line 7.

@pgasiorowski
Copy link
Author

I noticed something wrong about this "fix" so please do not merge yet.

I have three apps, all ember-cli 0.1.1
Two of them work fine but one attempts to load this for some reason:
http://192.168.42.160:4200/192.168.42.160:35729/livereload.js?snipver=1

@buschtoens
Copy link
Contributor

Any updates on this one? If this dosen't work I'd propose the following dynamic alternative:

function contentFor(type) {
  var liveReloadPort = process.env.EMBER_CLI_INJECT_LIVE_RELOAD_PORT;
  var liveReloadHost = process.env.EMBER_CLI_INJECT_LIVE_RELOAD_HOST;

  if (liveReloadPort && type === 'head') {
    if (liveReloadHost) {
      return '<script src="//'+ liveReloadHost +':' + liveReloadPort + '/livereload.js?snipver=1" type="text/javascript"></script>';
    } else {
      return '<script type="text/javascript">(function(d,l,p,s){s=d.createElement("script");s.type="text/javascript";s.async=true;s.src=l.protocol+"//"+l.hostname+":"+p+"/livereload.js?snipver=1";d.getElementsByTagName("head")[0].appendChild(s)})(document,document.location,' + liveReloadPort  + ')</script>';
    }
  }
}

If there is no EMBER_CLI_INJECT_LIVE_RELOAD_HOST supplied, this will inject the script below.

<script type="text/javascript">
  (function(d, l, p, s) {
    s = d.createElement("script");
    s.type = "text/javascript";
    s.async = true;
    s.src = l.protocol + "//"
      + l.hostname     + ":"
      + p
      + "/livereload.js?snipver=1";

    d.getElementsByTagName("head")[0].appendChild(s);
  })(document, document.location, 12345);
</script>

This script takes the current hostname of the page, which is most likely the livereload host as well. At least this makes way more sense than defaulting to localhost. The script is contained in an anonymous function and doesn't leak any globals.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants