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

后端 未结 3 608
面向向阳花
面向向阳花 2021-01-06 00:51

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

  

        
3条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-06 01:23

    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" %}
      
    {% 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)

提交回复
热议问题