有没有一种方法可以查看本地是否在服务杰基尔网站? [英] Is there a way to check whether a jekyll site is being served locally?

查看:57
本文介绍了有没有一种方法可以查看本地是否在服务杰基尔网站?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅在本地运行jekyll serve时,我想将以下行添加到我的head.html中:

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.

推荐答案

替代解决方案(例如,如果您将Jekyll网站托管在自己的服务器上而不是GitHub Pages上):

您可以像这样在配置文件_config.yml中设置一个值:

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

environment: prod

然后,您可以拥有另一个覆盖相同值的配置文件,我将其称为config_dev.yml:

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

environment: dev

当您仅调用jekyll build时,它将使用实际配置文件中的值prod.

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

第二个配置文件中的值将覆盖第一个配置文件中的值,因此environment将设置为dev.

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

然后您可以按照David的回答中所述进行相同操作:

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 %}


您可以在我的博客的源代码中看到示例:

  • _config.yml
  • _config_dev.yml
  • Windows批处理文件在开发"中运行jekyll build模式
  • 布局文件,我在其中使用environment变量执行以下操作:
    • 在开发人员模式下禁用Google Analytics(分析)
    • 更改我从其中加载JS和CSS文件的URL
      (开发模式:本地服务器/生产模式:CDN)
    • _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)

      这篇关于有没有一种方法可以查看本地是否在服务杰基尔网站?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆