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

4

15

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?

Pageantry answered 21/5, 2013 at 21:55 Comment(1)
Why can't you use relative links?Collagen
T
4

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/

Tulle answered 26/5, 2013 at 20:49 Comment(1)
This is fairly convoluted. Tommsy64's answer of checking jekyll.environment == "production" works perfectly well and is clean.Subjunction
I
29

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.

Immersed answered 18/7, 2016 at 22:12 Comment(1)
Note that Netlify doesn't set this automatically; you'll need to add it in Site Settings → Build and Deploy → EnvironmentCrossopterygian
D
26

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
Dispersoid answered 14/12, 2013 at 19:22 Comment(1)
Great idea with using an override configuration file. I created a _config-dev.yml override file, put "mode:development" in it, and it works perfectly to define when I am in development mode. Thanks!Scot
T
4

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/

Tulle answered 26/5, 2013 at 20:49 Comment(1)
This is fairly convoluted. Tommsy64's answer of checking jekyll.environment == "production" works perfectly well and is clean.Subjunction
F
3

This also worked for me:

$ JEKYLL_ENV=production jekyll serve
Freewheel answered 20/11, 2016 at 23:54 Comment(1)
If you define a custom build.command in your netlify.toml, e.g. sh netlify.sh, then you can prepend the jekyll command as suggested with JEKYLL_ENV=... easily.Tagmeme

© 2022 - 2024 — McMap. All rights reserved.