gohugoio / hugo

The world’s fastest framework for building websites.
https://gohugo.io
Apache License 2.0
73.92k stars 7.41k forks source link

baseURL trailing slash should be consistent between site setting and flag #3262

Closed RickCogley closed 7 years ago

RickCogley commented 7 years ago

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

bep commented 7 years ago

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.

RickCogley commented 7 years ago

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

RickCogley commented 7 years ago

@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!

vielmetti commented 7 years ago

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

vielmetti commented 7 years ago

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

vielmetti commented 7 years ago

Casper theme has been updated with changes.

github-actions[bot] commented 2 years ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.