How to detemine if jekyll running locally or in production site?

前端 未结 4 1757
谎友^
谎友^ 2021-02-13 03:25

There is a config param in jekyll called production_url. I can\'t find any information on how to use it.

Ideally i would like to be able to generate permalinks with loc

相关标签:
4条回答
  • 2021-02-13 04:07

    I don't see the variable production_url in the current release (v1.4.1), so this may be a dated question--but I was just looking for this answer myself. There is a baseurl property that can be set with a flag and so to change the path to your files, but it only adjusts the relative path.

    jekyll serve --baseurl '/blog'
    

    What you can do is to use the -config option to specify a configuration file for development.

    Jekyll Documentation

    Your production configuration variables are defined in _config.yml. One option is to create a separate configuration file for development.

    --config _config-dev.yml
    

    You can also (as I do) override variables defined in a second configuration file.

    --config _config.yml,_config-dev.yml
    

    If you use the liquid templates for site links like this:

    <link rel="stylesheet" href="{{ site.base_url }}/stylesheets/blog.css">
    

    then you can override the base_url property during local devlopment

    base_url:           http://localhost:4000
    

    and run jekyll in "Development"

    jekyll serve -w --config _config.yml,_config-dev.yml
    
    0 讨论(0)
  • 2021-02-13 04:08

    When you build your Jekyll website, it’s possible to specify the environment it’s using for the build with the JEKYLL_ENV environment variable:

    $ JEKYLL_ENV=production jekyll build
    

    If you don’t set JEKYLL_ENV explicitly, it defaults to development.

    {% if jekyll.environment == "production" %}
        // Production environment.
    {% endif %}
    

    Github Pages automatically sets the environment to production.

    0 讨论(0)
  • 2021-02-13 04:09

    This also worked for me:

    $ JEKYLL_ENV=production jekyll serve
    
    0 讨论(0)
  • 2021-02-13 04:24

    jekyll serve will call jekyll build, so you can't really use those two as a way to output different URL schemes.

    I built a Jekyll plugin that does this with a Liquid Filter and one user defined variable in your _config.yml called mode.

    You set the mode to development or production and the plugin takes care of the rest in your templates.

    So in your template you could have a URL such as:

    <img src="{{ '/img/dog.jpg' | to_absurl }}" />
    

    When mode is development, upon Jekyll build/serve you will get a relative URL:

    <img src="/img/dog.jpg" />
    

    Locally this would be accessed as: http://0.0.0.0:4000/img/dog.jpg

    When mode is production, upon Jekyll build/serve you will get an absolute URL:

    <img src="http://www.domain.tld/img/dog.jpg" />
    

    The http://www.domain.tld is what you have set in _config.yml -> url variable.

    You can see more details on the plugin here:

    http://jhaurawachsman.com/2013/jekyll-url-helper-filter-plugin/

    0 讨论(0)
提交回复
热议问题