7

I'd like to add the following line to my head.html solely when running jekyll serve locally:

  <script src="http://127.0.0.1:35729/livereload.js?snipver=1"></script>

I'm thinking of using some simple liquid check if possible.

oblitum
  • 11,380
  • 6
  • 54
  • 120

3 Answers3

15

When you do a jekyll serve locally the default {{ jekyll.environment }} variable is set to "development".

You can then do a simple :

{% if jekyll.environment == "development" %}
  <script src="http://127.0.0.1:35729/livereload.js?snipver=1"></script>
{% endif %}

If you want to run jekyll on another server, with another environment value, you can set a JEKYLL_ENV system environment variable to whatever you want.

Setting this variable at runtime can be done like this :

JEKYLL_ENV=production jekyll serve

Note : On Github Pages, jekyll.environment is set to production.

David Jacquel
  • 51,670
  • 6
  • 121
  • 147
  • 1
    It seems it didn't work, the script tag ends up in page sources when running `jekyll serve` as well as running `jekyll build`. – oblitum Jul 08 '15 at 04:21
  • Where are you doing this ? Locally, on a remote server, on github ? – David Jacquel Jul 08 '15 at 10:16
  • I've edited my answer to explain how to set the environment variable. – David Jacquel Jul 08 '15 at 12:07
  • Ah, interesting. I'm putting my site on GitHub, but I'm compilling it locally and pushing that since I'm using octopress 3. It seems I'll have to have that variable set in my machine, but since it's a web site to be edited by many hands/machines, I'm still unsure about the best approach. Maybe just letting the script tag hanging there would be the simplest. – oblitum Jul 08 '15 at 15:16
  • In an Octopress deploy schema, Christian's answer is the one. Use multiple configuration files. – David Jacquel Jul 08 '15 at 15:52
9

Alternative solution (for example, if you're hosting your Jekyll site on your own server and not on GitHub Pages):

You can set a value in the config file _config.yml like this:

environment: prod

Then, you can have another config file which overrides the same value, I'll call it config_dev.yml:

environment: dev

When you just call jekyll build, it will use the value prod from the real config file.

But when you build your site on your local machine for testing, you pass both config files in this order:

jekyll build --config _config.yml,_config_dev.yml

The value from the second config file will override the value from the first config file, so environment will be set to dev.

And then you can do the same as described in David's answer:

{% if site.environment == "dev" %}
  <script src="http://127.0.0.1:35729/livereload.js?snipver=1"></script>
{% endif %}

You can see an example in the source code of my blog:

Christian Specht
  • 35,843
  • 15
  • 128
  • 182
  • This is also an equally good solution, but as I've commented in the other answer I guess I'll leave the script line hanging around. – oblitum Jul 08 '15 at 15:19
0

To preview your site with drafts, run jekyll serve or jekyll build with the --drafts switch. Each will be assigned the value modification time of the draft file for its date, and thus you will see currently edited drafts as the latest posts.

Did u try this?

https://jekyllrb.com/docs/posts/

Ashok Koyi
  • 5,327
  • 8
  • 41
  • 50