"JEKYLL_ENV == production" is always true

I have an environment with two config files — one for local dev, one for production. My local dev config ignores a few files that enlarge the build times. These are pages that I really only need for production, like the sitemap, but I also have ignored an HTML page that is an index of all 150+ page nodes on the site (which will only increase in size).

Recently, my if statements have worked as expected but this morning they do not. In my navigation, I am using an if to check the JEKYLL_ENV variable and either output the link or not, like this:

{%- if JEKYLL_ENV == production -%}
  <a href="{% link all-properties.html %}">Architecture</a>
{%- else -%}
  <a href="#">Architecture</a>
{%- endif -%}

Previously, this worked. In my localhost, the link was not written but in production it was. Today, no matter the command, the build fails on this if statement. These Terminal commands all fail:

jekyll build
jekyll serve
JEKYLL_ENV=stage jekyll serve
JEKYLL_ENV=stage jekyll serve --config _config.yml --I

# All produce the error:
# Liquid Exception: Could not find document 'all-properties.html' in tag 'link'. Make sure the document exists and the path is correct. in /Users/jhogue/github/artinruins-com/_layouts/default.html

It seems that JEKYLL_ENV == production is always true. This is the ONLY instance of that link on the entire site. If I remove the {% link %}, jekyll serve works as expected.

I have not updated any dependencies. I could get around this by not using the {% link %} directive, but that seems silly as this worked previously.

Any pointers or different syntax that I can try?

Your syntax is incorrect.

Use jekyll.environment and you must quote production or your are referring to a variable named production.

{% if jekyll.environment == 'production' and site.google_analytics %}
    {% include google-analytics.html %}
{% endif %}
JEKYLL_ENV=production bundle exec jekyll build
1 Like

Interesting. I previously had the quotes in place, and a jekyll serve process worked, but when I ran my production command, the if statement was not evaluating to true:

# If this command:
JEKYLL_ENV=production jekyll build --config _config_prod.yml

# This does NOT evaluate to true and an empty link renders in the production env
{%- if JEKYLL_ENV == 'production' -%}
  <a href="{% link all-properties.html %}">Architecture</a>
{%- else -%}
  <a href="#">Architecture</a>
{%- endif -%}

I did not try to encase the production env. command in quotes as well, but that might be what I need to do (though you say that should not be needed).

Just confirmed that with a production command using quotes, I still get empty links. SO it seems like my next best bet, instead of fighting this fight, is to NOT use the {% link %} feature and instead do something that will create broken links in my staging env — only because the destination file will not be rendered in Staging.

So to confirm:

# If this command:
JEKYLL_ENV='production' jekyll build --config _config_prod.yml

# This does NOT evaluate to true and an empty link renders in the production env
{%- if JEKYLL_ENV == 'production' -%}
  <a href="{% link all-properties.html %}">Architecture</a>
{%- else -%}
  <a href="#">Architecture</a>
{%- endif -%}

I’m going to give up and do this without {% link %}. Thanks for talking this through with me.

Change this as in my comment before.

-JEKYLL_ENV == 'production'
+jekyll.environment == 'production'
1 Like

Try view a value if the if statement doesn’t work. This is good debugging approach for Jekyll in general

JEKYLL_ENV value: {{ JEKYLL_ENV | inspect }}

jekyll.environment value: {{ jekyll.environment | inspect }}

2 Likes

Oh wow my syntax was WAY off… yikes. I missed that change in reviewing your answer.

1 Like