How can I easily manage different base URL of Jekyll webpage on localhost and remote server?
Asked Answered
R

3

6

On my computer, I access my test webpage on URL http://127.0.0.1:4000. On server, it will be on GitHub pages, that means https://username.github.io/repo-name/.

In the _config.yml I see following relevant settings:

# Uncomment if you are planning to run the blog in a subdirectory
# Note - if you enable this, and attempt to view your site locally you have to use the baseurl in your local path.
# Example, you must use http://localhost:4000/path/to/blog
#baseurl: /path/to/blog 
baseurl: 

# The URL of your actual domain. This will be used to make absolute links in the RSS feed.
url: http://yourdomain.com/

So for GitHub server I need it to be:

baseurl: /repo-name/
url: https://username.github.io/

But on localhost, it must be:

baseurl: 
url: http://127.0.0.1:4000

These settings are necessary because without them, I will get 404 errors for resources that are using relative paths:

<script src="js/ghapi.js"></script>

"NetworkError: 404 Not Found - http://127.0.0.1:4000/download/js/ghapi.js"

The path should be http://127.0.0.1:4000/js/ghapi.js but since the page was /download it was added to relative URL of the script file.

I need to deal with this issue, how do I do that?

Refuel answered 5/5, 2016 at 11:44 Comment(2)
Would be another config.yml for the local development defined as a serve parameter a solution for you? There is a similar question on SO.Corneliacornelian
Possible duplicate of Change site.url to localhost during jekyll local developmentMagyar
R
13

The best solution was to have two config files. The additional one, debug.yml, overrides some settings from the basic one. Both setting files can be loaded using this command:

jekyll serve --config _config.yml,debug.yml

The debug file can contain:

name: MySite [DEBUG MODE]
debug: true
url: http://127.0.0.1:4000

The advantage here is that no setting files need to be changed, you just use different command to run jekyll.

Refuel answered 7/5, 2016 at 10:1 Comment(0)
I
1

For me the best option is having in config.yml the baseurl used in Github pages and when you launch your site locally, override this value with an empty one:

bundle exec jekyll serve --baseurl=

That way, the site will work on localhost and in ghpages.

Intelligent answered 16/5, 2021 at 11:46 Comment(0)
N
0

you can add a branch and change url line in config.yml
url: http://127.0.0.1:4000

Nationalism answered 6/5, 2016 at 22:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.