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

拈花ヽ惹草 提交于 2019-12-21 07:29:11

问题


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 local url when it is being run with serve param and with production url when run with build param.

How could I do that?


回答1:


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/




回答2:


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.




回答3:


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



回答4:


This also worked for me:

$ JEKYLL_ENV=production jekyll serve


来源:https://stackoverflow.com/questions/16680153/how-to-detemine-if-jekyll-running-locally-or-in-production-site

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