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

自闭症网瘾萝莉.ら 提交于 2019-11-30 22:57:36

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:

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.

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/

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!