1

I'm using github to host my blog built with jekyll.

I read in another post and the documentation that I need to change site.baseurl to site.github.url to get my static resources to be served. So that's what I did and now everything is working. See diff below between master and gh-pages:

enter image description here

It wasn't too much of a pain to do, I just project-wide replaced using atom; however, I'm wondering, is there a better workaround? Ideally I'd like my workflow to be so I can just work on my blog using a normal branching model and then merge with gh-pages as if it's a release branch and not have to worry about making the search and replace every time.

Thanks for your help :)

Edit: So strange, I just merged master with gh-pages and it seems like git just automagically handled the whole thing for me. So different question. Does git really know not to change site.github.url to site.baseurl when I make the merge? How does that work?

Community
  • 1
  • 1
mbigras
  • 7,664
  • 11
  • 50
  • 111
  • 1
    Git merges changes. So if you have changed the url on the `gh-pages` branch, but never touch it on the `master` branch, changes you merge in from `master` onto `gh-pages` will not include a change of the url, so the url used on `gh-pages` will stay the way it is. – poke Sep 10 '16 at 10:56

3 Answers3

4

As of Jekyll 3.3, if you're using the latest version of the github-pages gem, you can now just use site.url everywhere. In development it will automatically be set to localhost and in production it will be set to your appropriate Github URL.

Jeff Bowen
  • 5,904
  • 1
  • 28
  • 41
  • which was quite an unfortunate decision, as many development environments won't have url set to localhost (e.g. those working in VMs) – BangTheBank Feb 19 '19 at 14:02
1

You can do this by using jekyll.environment:

{% if jekyll.environment == "production" %}
  <a class="post-link" href="{{ post.url | prepend: site.github.url }}">{{ post.title }}</a>
{% else %}
  <a class="post-link" href="{{ post.url | prepend: site.baseurl }}">{{ post.title }}</a>
{% endif %}

In your build command you then have to set JEKYLL_ENV=production . This is done automatically by GitHub. For other platforms, you may have to do this manually (in a Rakefile for example):

JEKYLL_ENV=production jekyll build

By default, JEKYLL_ENV equals development.

SevenEleven
  • 2,326
  • 2
  • 32
  • 32
0
  1. You can leave baseurl empty (baseurl: "") in your _config.yml.
  2. In this case, you don't need to prepend baseurl to post_url

There has been some confusion around baseurl and url, you might want to read this clarification by Jekyll's maintainer: https://byparker.com/blog/2014/clearing-up-confusion-around-baseurl/

site.url: https://domain.com # your domain
site.baseurl: "/blog" # the path to your website (optionnal)
site.github.url: https://username.github.io # your GitHub URL
DirtyF
  • 661
  • 7
  • 10