Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

With `server` deprecated, how can I reliably tell if I'm in Dev or Prod mode? #1219

Closed
philipwalton opened this Issue · 15 comments

7 participants

@philipwalton

Before the release of version 1, I could check if site.server was true and then include development only features (or exclude production only features) based on that conditional.

Here's a simple example:

{% if site.server %}
  {% include local-testing-scripts.html %}
{% endif %}

But now it seems that I have no way to reliably do this check (unless I'm missing something).

The only thing I can think of is to set some random config variable (like port), and then make the assumption that I'm in dev mode unless port is set to whatever number I choose to mean production mode.

But that seems hacky. Is there an officially supported way to do what I'm after?

@mattr-
Owner
@philipwalton

Right, but if it's in _config.yml it'll show up when I do both jekyll serve and jekyll build, which defeats the purpose. I also can't pass in a config variable via the command line (e.g. jekyll serve --env dev), so that won't work.

Anyway, thanks for the quick reply. I guess I have my answer. However, my personal opinion is that an environment variable would be very useful to a lot of people.

@parkr
Owner

Someone asked about this before – I recommended that they use a plugin to handle this:

$ JEKYLL_ENV=production jekyll build
class Jekyll::Site
  attr_accessor :env
end

class Env < Jekyll::Generator
  priority :highest
  def generate(site)
    site.env = ENV['JEKYLL_ENV'] || 'development'
  end
end

Or something like that.

@parkr parkr closed this
@philipwalton

@parkr, thanks that's helpful.

@mdo

@parkr What about folks who cannot use plugins with GitHub Pages?

@parkr
Owner

@mdo What you generate locally is also a valid Jekyll site so push your _site!

@mdo

@parkr I fully understand that any local Jekyll server is a valid Jekyll site that can use plugins, but as I understand it GitHub Pages triggers --safe on deploy, thus nullifying plugins. My use case is for Bootstrap where I want to show our ads and analytics code only in production and not in local development. I'd rather not have to add to the deployment process if I can avoid it as that's the primary appeal of Jekyll in GitHub Pages.

@philipwalton

@mdo that was my use-case as well. I didn't want my Google Analytics code to run when developing locally, but obviously wanted it to run when deployed. And for at least one of my sites, I couldn't use a custom plugin for the reason you just mentioned.

The (kinda hacky) solution I ended up going with was to check site.port in my conditional. I wrote a rake task to always preview my site on port 1234. That allows me to say: insert this Google Analytics snippet unless site.port is 1234.

@ixti

Why not add environment flag (and respect JEKYLL_ENV) environment variable out of the box. This is a trivial, safe change which will make life easier for lots of people. Like we have --drafts flag which is also helpful for local-only development.

@parkr
Owner

I'd be happy to add that. I get the appeal with regard to site URL and several other variables. @benbalter, think we could add an "jekyll_env" option which defaults to "development" and can be overwritten as "production" in GH Pages's config?

@benbalter
Owner

Great question. Two possible routes:

1. Detecting GitHub Pages

When Pages builds a Jekyll site, there are certain settings we override by default. It's not currently documented, but there's already a flag we use internally which you're welcome to use:

GH_ENV: gh_pages

If you look for site.GH_ENV, you should be able to tell if you're on Pages, although, we can't guarantee that will be there forever. If there's a better flag, glad to add it to the documentation. Easy enough fix.

2. Detecting Development

The other, arguably smarter route, that more closely matches the original behavior would be to pass a development config file. Let's say your standard _config.yml has environment: production in it. Awesome. That will be available to you on Pages (or whenever you run jekyll build). Now let's say you also have a _config_dev.yml with environment: development. To develop, you'd run jekyll serve --config _config.yml,_config_dev.yml, and you'd get site.environoment as development locally and production in "production". Bonus points, you can even script it.

An env var

Seems simple enough to expose something if Jekyll is running as jekyll serve which presumably is always going to be locally. If the site's on localhost:4000, Jekyll should be able to detect that and expose that within site, right?

. My use case is for Bootstrap where I want to show our ads and analytics code only in production and not in local development.

@mdo ping me, more than glad to help out with this.

@parkr parkr reopened this
@robwierzbowski

We use @benbalter's option 2 in the Yeoman generator for Jekyll and it works quite well (code here).

@mattr-
Owner
@parkr
Owner

What do we think about this?

This was referenced
@parkr
Owner

Use {{ jekyll.environment }}.

@parkr parkr closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.