Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to check whether a jekyll site is being served locally?

Tags:

jekyll

liquid

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.

like image 202
pepper_chico Avatar asked Jul 07 '15 16:07

pepper_chico


People also ask

How do I run jekyll locally?

Step 1: Create a local repository for your Jekyll site. Step 2: Install Jekyll using Bundler. Step 3 (optional): Generate Jekyll site files. Step 4: Build your local Jekyll site.

What does the bundle exec jekyll build command do?

jekyll build or jekyll b - Performs a one off build your site to ./_site (by default). jekyll serve or jekyll s - Builds your site any time a source file changes and serves it locally. jekyll clean - Removes all generated files: destination folder, metadata file, Sass and Jekyll caches.


2 Answers

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.

like image 189
David Jacquel Avatar answered Oct 19 '22 02:10

David Jacquel


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:

  • _config.yml
  • _config_dev.yml
  • Windows batch file to run jekyll build in "dev" mode
  • Layout file, where I'm using the environment variable to:
    • disable Google Analytics in dev mode
    • change the URLs from where I'm loading JS and CSS files
      (dev mode: local server / prod mode: CDNs)
like image 45
Christian Specht Avatar answered Oct 19 '22 04:10

Christian Specht