Hugo: baseURL trailing slash should be consistent between site setting and flag

Created on 2 Apr 2017  路  6Comments  路  Source: gohugoio/hugo

Hello - it's been a while.

If I set a baseURL in my config.toml, such as http://www.mysite.com, when I use {{ .Site.BaseURL }}, no trailing slash is appended, so I'd compensate by adding one in the template.

Then, say if I want to publish the same content to http://anothersite.com/~myuser, I can override the baseURL in the hugo command when I generate like:

 hugo --config="/path/to/my/config.toml" --baseURL="http://anothersite.com/~myuser" -s /path/to/my/site/ -d /tmp/anothersite.com.myuser

...but, when doing this, it appends a trailing slash, so I don't need to compensate.

Should this not be consistent?

Using:
Hugo Static Site Generator v0.20-DEV-73C1C7B6 darwin/amd64 BuildDate: 2017-04-02T16:28:29+09:00

Bug

Most helpful comment

@bep as you say, that works perfectly.

For future reference for searchers, doing:

 <img src="{{ "img/mylogo.jpg" | relURL }}" class="this that" alt="Logo">

... for the http://anothersite.com/~myuser site, gives:

 <img src="/~myuser/img/mylogo.jpg" class="this that" alt="Logo">

... and for the http://www.mysite.com site, gives:

 <img src="/img/mylogo.jpg" class="this that" alt="Logo">

Thanks again!

All 6 comments

It should be consistent. But manually creating URLs using {{ .Site.BaseURL }} is fragile, esp. for themes, and not recommended.

Use absURL and friends, and you should not have to worry about slashes.

thanks @bep, I'll give it a try!

@bep as you say, that works perfectly.

For future reference for searchers, doing:

 <img src="{{ "img/mylogo.jpg" | relURL }}" class="this that" alt="Logo">

... for the http://anothersite.com/~myuser site, gives:

 <img src="/~myuser/img/mylogo.jpg" class="this that" alt="Logo">

... and for the http://www.mysite.com site, gives:

 <img src="/img/mylogo.jpg" class="this that" alt="Logo">

Thanks again!

This change is a bug fix and it also will break configurations that relied inadvertently on the old buggy code. It's a one-byte fix if you need the trailing slash. If you need it and don't use it, you'll get this behavior:

https://discuss.gohugo.io/t/hugo-0-20-upgrade-page-template-breaks/6097

https://github.com/vjeantet/hugo-theme-casper/issues/84 notes changes suggested to the Casper theme for 0.20.

Casper theme has been updated with changes.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sigma picture sigma  路  3Comments

moorereason picture moorereason  路  3Comments

arikroc picture arikroc  路  3Comments

VoidingWarranties picture VoidingWarranties  路  3Comments

bep picture bep  路  3Comments