mbigras
mbigras

Reputation: 8055

How to avoid having to change site.baseurl -> site.github.url between master/gh-pages?

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?

Upvotes: 1

Views: 1278

Answers (3)

Jeff Bowen
Jeff Bowen

Reputation: 6162

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.

Upvotes: 4

DirtyF
DirtyF

Reputation: 681

  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

Upvotes: 0

SevenEleven
SevenEleven

Reputation: 2474

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.

Upvotes: 1

Related Questions